954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

print the fields of a structure

Hi I want to print in the screen only the fields of some of the data that is in the structure, Doom3 like: the name for example,
or the field time in FIFA, or the field memory in word
How can i do this ?

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct proceso{
         char name[20];
         int  memory;
         int  time;
         int  files;
           };

struct proceso Doom3   ={"Doom3", 500, 4,5};
struct proceso Hades    ={"Hades", 100, 2,1};
struct proceso Word     ={"Word" ,  50, 1,1};
struct proceso FIFA       ={"FIFA" ,  10, 3,1};
struct proceso Doom2   ={"Doom2", 400, 5,2};



printf("\n ??????????
endsamsara
Light Poster
36 posts since Mar 2007
Reputation Points: 8
Solved Threads: 1
 

you have to print each field individually

printf("name: %s\n", Doom3.name);
printf("memory: %d\n", Doom3.memory);
...
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

'.' is the operatir which you are looking at.
'->' is the operator which you need when you dereference the struct pointer variable.

Like for an example

struct node
{
    int data;
}

struct node *d;
struct node d1;

printf("%d\n", d->data); // you could also use (*d).data here. The reason for this is operator precedences
printf("%d\n", d1.data);


~ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

consider the following:

typedef struct 
{
   char name[20];
   int memory;
   int time;
   int files;
} proceso;        // proceso is a "type" of struct
                  // defined as having the above elements
 
proceso titulos[MAX_TITLES] = {       // titulos is an instance of proceso
   {"Doom3", 500, 4,5},               // declared as an array
   {"Hades", 100, 2,1},               // and containing the following values
   {"Word" , 50, 1,1},
   {"FIFA" , 10, 3,1},
   {"Doom2", 400, 5,2},
// ...
// question:  how many is "MAX_TITLES" ?
};

//....
// print fields like so

for (i=0; i<MAX_TITLES; i++)
{
   printf("title name: %s (%d MB)\n",titulos[i].name, titulos[i].memory);
}

// etc...
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Hi I want to print in the screen only the fields of some of the data that is in the structure, Doom3 like: the name for example, or the field time in FIFA, or the field memory in word How can i do this ?

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct proceso{
         char name[20];
         int  memory;
         int  time;
         int  files;
           };

struct proceso Doom3   ={"Doom3", 500, 4,5};
struct proceso Hades    ={"Hades", 100, 2,1};
struct proceso Word     ={"Word" ,  50, 1,1};
struct proceso FIFA       ={"FIFA" ,  10, 3,1};
struct proceso Doom2   ={"Doom2", 400, 5,2};
printf("\n%s",proceso.Doom3.name);

//try like this

sasik
Newbie Poster
1 post since Dec 2009
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You