   Understanding structs
  Hi guys: I have a C exam in some days, but I really lag on understanding structs, somebody can give me a quick understanding about them? how the work? but with C, because she uses like printf, and all about CThank you mates!   
  According to my old school TurboC book, "A structure consists of a number of data items - which need not be of the same type - grouped together." What does your textbook say?   ;  I think you managed to phrase your question so that it falls under the "no homework help" thing...BUTa struct is a way of encapsulating several basic variable types into a new container variable type:struct Foo{inti;float   f;double  d;string  s;};Then allowing you to make variables with those "members" so you can later access all the parts through one variable instead of passing arround all the parts:OutPutString ( Foo &f ){cout<<f.s<<endl;}int main (){...Foo my_foo;my_foo.i = 1; //store something in the integer member of foomy_foo.s = "hello world";OutPutString ( my_foo );};  oh ok great guys, my instructor uses malloc to i don't know for, i want to learn more about that and the use of struct with pointers, is that hard?thx guys!   ;  KulSeran has a point about homework, but he seems to have overlooked your remark about printf. #include <malloc.h>typedef struct tagMyStruct { unsigned long one; unsigned long two; unsigned long tri;}MyStruct, *PMyStruct;int main(void){ MyStruct *pmystruct = malloc(sizeof(*pmystruct)); if ( NULL == pmystruct ) {  printf("error no memory.\n"); } pmystruct->one = 1; pmystruct->two = 2; pmystruct->tri = 3; printf("%d %d %d\n", pmystruct->one, pmystruct->two, pmystruct->tri); free(pmystruct);  return 0;}Note that malloc returns void *, so there's no need for a typecast. That said the typecast is often used in that situation. My advice is to follow the style laid down by your teacher. If that means using the typecast, then use it.  MyStruct *pmystruct = (MyStruct *)malloc(sizeof(*pmystruct));You can fuss with the peculiarities of the language when you've got a better grasp of it. ;Quote:Original post by yayo_99oh ok great guys, my instructor uses malloc to i don't know for, i want to learn more about that and the use of struct with pointers, is that hard?thx guys!If you're unsure about something used in C, just search google for "c tutorial" and then the word (malloc for example). http://www.google.ca/search?q=c+tutorial+struct&sourceid=mozilla-search&start=0&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:officialHeres a good one: http://cplus.about.com/od/beginnerctutoria1/l/aa041602a.htm   ;  thx thx guys, i have these piece of code:#include<cstdio>#include<cstdlib>#include<conio.h>using namespace std;struct tm {  int hours, min, sec;};void print(struct tm *t) {printf("%i:%i:%i\n", t->hours, t->min, t->sec);t->hours=12;t->min=25;t->sec=15;}void main() {struct tm time;time.horas=11;time.minutos=20;time.segundos=10;print(&time);printf("%i:%i:%i\n", time.hours, time.min, time.sec);system("PAUSE");}so here she (instructor) makes a struct with hours, minutes and secs, then on main she change the name of the structure tm to time, then gives numbers to each variable of the struct by the new name "time", then she goes print(&time) ... i lak on that, passing variables?... ok then print the time, then finish. Am I right?   ;  She doesn't change the name of the struct, she declares an instance of it named time. Because the structure is declared as a local - the variable represents the entire structure - values are assigned to it using the . (dot) notation rather than -> (pointer) notation. Had she allocated the memory for it using malloc, she would have had to use the -> pointer notation to assign values to the various members of the structure. Because it's easier to pass a pointer as an argument to a function, she writes the print function such that it accepts a pointer and from that follows the use of -> pointer notation to access the values stored in the structure. The print function is called using the & (address of) operator. The address of a structure is a pointer.   ;  Thx LessBread i understand what you say. How does the passing variable works? the main(THINGS)?thx!   ;http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/functions.html