Question on structs

This is a discussion on Question on structs within the C Programming forums, part of the General Programming Boards category; Can someone explain what this small piece of code means. Code: struct Record { int ID; int EntryTime; Position next; ...

  1. #1
    Unregistered
    Guest

    Question on structs

    Can someone explain what this small piece of code means.

    Code:
    struct Record {
       int ID;
       int EntryTime;
       Position next;
    };
    Is "Position" a newly created type? Thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  3. #3
    Unregistered
    Guest
    Thanks Prelude!

    Another question

    Code:
    struct Record {
       typedef struct Record *r;
       typedef struct Record *Position;
    Is this a typedef for an existing struct as you posted earlier. I'm very confused!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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):

    Code:
    struct Record
    {   
        int ID;   
        int EntryTime;   
        struct Record *next;
    };
    It can be typedef'd and used like this:
    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);
    }
    By typedef'ing the struct, you can reference it through Record_t, instead of "struct Record".
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    struct Record {
       typedef struct Record *r;
       typedef struct Record *Position;
    "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.

    >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:
    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;
    }
    It's all very fun when you get the hang of it.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    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!

  7. #7
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59
    that was nice prelude
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. Structs question
    By BigFish21 in forum C Programming
    Replies: 25
    Last Post: 04-23-2008, 10:57 PM
  3. Replies: 5
    Last Post: 02-20-2004, 09:36 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21