944,144 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 13783
  • C RSS
Mar 25th, 2007
0

print the fields of a structure

Expand Post »
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 ?

  1.  
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct proceso{
  8. char name[20];
  9. int memory;
  10. int time;
  11. int files;
  12. };
  13.  
  14. struct proceso Doom3 ={"Doom3", 500, 4,5};
  15. struct proceso Hades ={"Hades", 100, 2,1};
  16. struct proceso Word ={"Word" , 50, 1,1};
  17. struct proceso FIFA ={"FIFA" , 10, 3,1};
  18. struct proceso Doom2 ={"Doom2", 400, 5,2};
  19.  
  20.  
  21.  
  22. printf("\n ??????????
  23.  
Similar Threads
Reputation Points: 8
Solved Threads: 1
Light Poster
endsamsara is offline Offline
36 posts
since Mar 2007
Mar 25th, 2007
0

Re: print the fields of a structure

you have to print each field individually
  1. printf("name: %s\n", Doom3.name);
  2. printf("memory: %d\n", Doom3.memory);
  3. ...
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Dec 7th, 2009
-1
Re: print the fields of a structure
'.' 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
  1. struct node
  2. {
  3. int data;
  4. }
  5.  
  6. struct node *d;
  7. struct node d1;
  8.  
  9. printf("%d\n", d->data); // you could also use (*d).data here. The reason for this is operator precedences
  10. printf("%d\n", d1.data);

~ssharish
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Dec 7th, 2009
0
Re: print the fields of a structure
consider the following:

  1. typedef struct
  2. {
  3. char name[20];
  4. int memory;
  5. int time;
  6. int files;
  7. } proceso; // proceso is a "type" of struct
  8. // defined as having the above elements
  9.  
  10. proceso titulos[MAX_TITLES] = { // titulos is an instance of proceso
  11. {"Doom3", 500, 4,5}, // declared as an array
  12. {"Hades", 100, 2,1}, // and containing the following values
  13. {"Word" , 50, 1,1},
  14. {"FIFA" , 10, 3,1},
  15. {"Doom2", 400, 5,2},
  16. // ...
  17. // question: how many is "MAX_TITLES" ?
  18. };
  19.  
  20. //....
  21. // print fields like so
  22.  
  23. for (i=0; i<MAX_TITLES; i++)
  24. {
  25. printf("title name: %s (%d MB)\n",titulos[i].name, titulos[i].memory);
  26. }
  27.  
  28. // etc...
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Dec 8th, 2009
-1

print some of the fields of the structure

Click to Expand / Collapse  Quote originally posted by endsamsara ...
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 ?

  1.  
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct proceso{
  8. char name[20];
  9. int memory;
  10. int time;
  11. int files;
  12. };
  13.  
  14. struct proceso Doom3 ={"Doom3", 500, 4,5};
  15. struct proceso Hades ={"Hades", 100, 2,1};
  16. struct proceso Word ={"Word" , 50, 1,1};
  17. struct proceso FIFA ={"FIFA" , 10, 3,1};
  18. struct proceso Doom2 ={"Doom2", 400, 5,2};

  1. printf("\n%s",proceso.Doom3.name);
//try like this
Last edited by adatapost; Dec 8th, 2009 at 2:28 am. Reason: Improved formatting.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
sasik is offline Offline
1 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Flowchart help Please!
Next Thread in C Forum Timeline: Urgent Strings help!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC