print the fields of a structure

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 23
Reputation: endsamsara is an unknown quantity at this point 
Solved Threads: 0
endsamsara endsamsara is offline Offline
Newbie Poster

print the fields of a structure

 
0
  #1
Mar 25th, 2007
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.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: print the fields of a structure

 
0
  #2
Mar 25th, 2007
you have to print each field individually
  1. printf("name: %s\n", Doom3.name);
  2. printf("memory: %d\n", Doom3.memory);
  3. ...
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training
 
-1
  #3
14 Days Ago
'.' 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
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso
 
0
  #4
14 Days Ago
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2009
Posts: 1
Reputation: sasik is an unknown quantity at this point 
Solved Threads: 0
sasik sasik is offline Offline
Newbie Poster

print some of the fields of the structure

 
-1
  #5
14 Days Ago
Originally Posted by endsamsara View 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};

  1. printf("\n%s",proceso.Doom3.name);
//try like this
Last edited by adatapost; 14 Days Ago at 2:28 am. Reason: Improved formatting.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 3846 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC