Thread: Self referencing structure

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Self referencing structure

    I have a structure that contains a pointer to another instance of itself (like a linked list). However I'm getting an error that says I'm missing a ";" when I'm not...

    Code:
    typedef struct IOD {
      char Name[11];
      int  Request_Type;
      char *Transfer_Buffer;
      int  *Transfer_Counter;
      IOD  *Next_IOD;
    } IOD;
    I think I need some sort of prototype struct but I'm unsure how to do this.
    Last edited by Bladactania; 04-22-2009 at 11:33 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that the typedef name IOD only exists after the struct definition.

    Option #1:
    Code:
    typedef struct IOD IOD;
    
    struct IOD {
      char Name[11];
      int  Request_Type;
      char *Transfer_Buffer;
      int  *Transfer_Counter;
      IOD  *Next_IOD;
    };
    Option #2:
    Code:
    typedef struct IOD {
      char Name[11];
      int  Request_Type;
      char *Transfer_Buffer;
      int  *Transfer_Counter;
      struct IOD  *Next_IOD;
    } IOD;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Pointer to a structure element

    I have a function that expects a int * as a parameter. I have an int inside a structure.

    Code:
    typedef struct myStruct {
      int myStructInt;
    } myStruct;
    
    
    myStruct test_struct;
    
    
    void test_fun(int * myInt) {
      // yadda yadda
    }
    
    int main() {
      test_fun(??????????);
    }
    How do I pass the address of the structure element? I tried &(test_struct.myStructInt).
    Last edited by Bladactania; 04-22-2009 at 11:49 AM. Reason: This was supposed to be a new thread!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bladactania
    How do I pass the address of the structure element? I tried &(test_struct.myStructInt).
    That should work, though it can be simplified to &test_struct.myStructInt.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, that's completely wrong now. The structure doesn't contain a pointer to a structure any more. You took it out and declared a type. Go back and read laserlight's post again.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  4. referencing structure variables
    By sballew in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 01:56 PM
  5. referencing C structure elements in assembly
    By BigAl in forum C Programming
    Replies: 7
    Last Post: 09-10-2001, 03:19 PM