Can someone explain what this small piece of code means.
Is "Position" a newly created type? Thanks in advance.Code:struct Record { int ID; int EntryTime; Position next; };
Can someone explain what this small piece of code means.
Is "Position" a newly created type? Thanks in advance.Code:struct Record { int ID; int EntryTime; Position next; };
>Can someone explain what this small piece of code means.
Record is a structure that contains three members: two integers and a user defined type.
>Is "Position" a newly created type?
It's hard to tell without more code, the most possible thing is that Position is another structure. But it could simply be a typedef for an existing type.
-Prelude
My best code is written with the delete key.
Thanks Prelude!
Another question
Is this a typedef for an existing struct as you posted earlier. I'm very confused!Code:struct Record { typedef struct Record *r; typedef struct Record *Position;
I might be wrong, but I'm pretty sure you can't typedef in the middle of a struct.
If you're looking for a struct that contains a pointer to itself (a classic linked list scenario):
It can be typedef'd and used like this:Code:struct Record { int ID; int EntryTime; struct Record *next; };
By typedef'ing the struct, you can reference it through Record_t, instead of "struct Record".Code:#include <stdio.h> typedef struct Record { int i; int EntryTime; struct Record *next; } Record_t; int main(void) { Record_t MyRecord; MyRecord.i = 10; printf ("MyRecord.i = %d", MyRecord.i); return (0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
"Oh yuck" was my first reaction to reading that. Yes, both r and Position are alternate names for struct Record * by using typedef. But, since typedef isn't allowed as a storage class for struct members this won't work too well.Code:struct Record { typedef struct Record *r; typedef struct Record *Position;
>I'm very confused!
It helps to think of typedef as a storage class, like static, and the new type name that is being created like a regular variable. For example, to declare a static integer named x you would say:
static int x;
But to create a new name x for int, you do the same thing except with typedef:
typedef int x;
In the first declaration, x is a variable of int and can be used to hold values. In the second declaration x is declared to be another name for int, so you can create variables of x and use them just like variables of int:
x i;
for ( i = 0; i < 10; x++ ) printf ( "%d\n", x );
The great thing about typedef is you can create names like this:
It's all very fun when you get the hang of it.Code:#include <stdio.h> typedef void (*FindRec) ( void ); static void print ( void ) { (void)puts ( "We're here through a function pointer" ); } static FindRec func ( void ) { return &print; } int main ( void ) { FindRec p = func(); p(); return 0; }
-Prelude
My best code is written with the delete key.
Thank you Hammer and Prelude. I was looking at some sample code on the internet and could not figure out the logic of the program. I guess I should be weary of my sources. Thanks again for your help!
that was nice prelude
" programming is 1% syntax and 99% logic "