Thread: c structs

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    c structs

    i'm used to using c++ classes and structs, and i don't know exaclty what to do with c-style structs.
    Code:
    typedef struct node_t {
      struct node* zero;
      struct node* one;
      struct node* parent;
      struct node* next;
      struct node* head;
      int frequency;
      int value;
    
      } node, *pnode;
    this is what i have. it seems to work until i try to change the node pointers to anything but NULL. my compiler, gcc, complains about "Dereferencing pointer to incomplete type" and "warning: assignment from incompatable pointer type"

    any ideas on what's wrong? i'm trying to create a linked list/binary tree. i don't know if i can use the pointers inside the struct because the struct hasn't yet been defined.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    try

    Code:
    typedef struct node_t
    {
        struct node_t* zero;
        struct node_t* one;
        struct node_t* parent;
        struct node_t* next;
        struct node_t* head;
        int frequency;
        int value;
    } node, *pnode;
    ::edit:: you should also probably reverse the naming to

    Code:
    typedef struct node
    {
        struct node* zero;
        struct node* one;
        struct node* parent;
        struct node* next;
        struct node* head;
        int frequency;
        int value;
    } node_t, *pnode;
    Last edited by no-one; 07-21-2002 at 12:09 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i got that part down. my problem now is in this function:
    Code:
    node_t* nth_node(node_t x[],int n) {
      node_t* temp;
      temp=x[0];
      while (n) {
        temp=temp->next;
        n--;
    
      }
      return temp;
    
    }
    i have an array declared like this:
    Code:
    node_t nodes[256];
    and the prototype's like this:
    Code:
    node_t* nth_node(node_t[],int);
    i'm calling the function like this:
    [code]
    nth_node(nodes,1)->left = nth_node(nodes,2);
    it's giving me "invalid type argument of '->'" and "warning: passing arg1 of nth_node with incompatable pointer type", and also an lvalue error. any ideas? thanks in advance

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    The problem could possibly be that
    nth_node(nodes,1)->left
    isn't returning the actual address of the original struct.

    Try changing the declaration to this:
    node_t* nth_node(node_t*,int);

    I'm not 100% on this though.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Note that temp is a pointer to struct and x[0] is a struct. You want temp to point to x[0], so it should be temp = &x[0].

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    Regarding this line:

    nth_node(nodes,1)->left = nth_node(nodes,2);

    The reason that you are getting these errors is that the compiler is expecting a variable as the lvalue -- a function call cannot serve as the lvalue.

    Another thing is that the '->' operator cannot have a function on its left side.

    Alternatively, you can try this:

    node_t Ptr_Temp = nth_node(nodes,1);
    Ptr_Temp -> Left = nth_node(nodes,2);

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    Sorry, that last line should be:

    Ptr_Temp -> left = nth_node(nodes, 2);

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    The reason that you are getting these errors is that the compiler is expecting a variable as the lvalue -- a function call cannot serve as the lvalue.

    Another thing is that the '->' operator cannot have a function on its left side.
    Hmmm......

    Then I wonder why this works
    Code:
    #include <stdio.h>
    
    typedef struct Person
    {
    	char name[20];
    	char surname[20];
    	struct Person* next;
    }Person;
    
    typedef Person* ptrPerson;
    
    ptrPerson function(ptrPerson temp)
    {
    	return temp;
    }
    
    int main()
    {
    	ptrPerson one = new Person;
    	
    	function(one)->next = NULL;
    	delete one;
    	
    	return 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Then I wonder why this works
    Probably 'cos it's not C, its C++. (guessing here!)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> Probably 'cos it's not C, its C++. (guessing here!)

    Close, but no cigar.

    It works 'cos C says that there's nothing wrong with that.

    I changed the code so it used malloc/free and changed the extension to ".C", and it still worked.

  11. #11
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    Shiro nailed it.

    second reguarding,

    nth_node(nodes,1)->left = nth_node(nodes,2);

    this is a perfectly legal line of code in C and C++, here is why.

    to break it down as simple as possible in sequence.

    Code:
    nth_node(nodes,1)->left = nth_node(nodes,2)
    
    the functions nth_node() is called first, it now reads like this.
    
    returned_pointer_to_1->left = returned_pointer_to_2;
    
    where 'returned_pointer_*' is the node_t* returned by the function nth_node().
    
    now,
    'returned_pointer_to_1' is first deferenced by '->' operator
    and then its member 'left' is accessed, it could be rewritten like so
    
    (*returned_pointer_to_1).left = returned_pointer_to_2;
    
    then asignment takes place, i think you understand the rest.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Sorry

    Sorry, I got that one wrong. That was my mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM