C struct / pointer confusion

This is a discussion on C struct / pointer confusion within the C Programming forums, part of the General Programming Boards category; Hello, I'm having a lot of trouble implementing a linked-list style stack in C. I am getting the following compiler ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    C struct / pointer confusion

    Hello,
    I'm having a lot of trouble implementing a linked-list style stack in C. I am getting the following compiler error:

    Code:
    stack.c:28: error: request for member "data" in something not a structure or union.
    I know what you're thinking... he used a '.' when he should have used a '->'. I don't think that this is my problem in this case, as I have attempted to compile with every combination in a "stab in the dark" debugging style (haha). Here is the code:

    Code:
    char* pop (stack_t *stack) {
    ...
    node *back;
    back = stack->back;
    ...
    char *return_val;
    return_val = back->data; //the line causing the error
    ...
    }
    Here is the defenition for stack_t and node:

    Code:
    typedef struct node *node; //so I can use self referencing structure members
    
    struct node {
    node *next;
    node *prev;
    char *data;
    };
    
    typedef struct  {
    node* front;
    node*back;
    int size;
    } stack_t;
    Can anyone help me out?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by ChipsHandon View Post
    Hello,
    I'm having a lot of trouble implementing a linked-list style stack in C. I am getting the following compiler error:

    Code:
    stack.c:28: error: request for member "data" in something not a structure or union.
    I know what you're thinking... he used a '.' when he should have used a '->'. I don't think that this is my problem in this case, as I have attempted to compile with every combination in a "stab in the dark" debugging style (haha). Here is the code:

    Code:
    char* pop (stack_t *stack) {
    ...
    struct node *back;
    back = stack->back;
    ...
    char *return_val;
    return_val = back->data; //the line causing the error
    ...
    }
    Here is the defenition for stack_t and node:

    Code:
    typedef struct node *node; //so I can use self referencing structure members
    
    struct node {
    struct node *next;
    struct node *prev;
    char *data;
    };
    
    typedef struct  {
    struct node* front;
    struct node*back;
    int size;
    } stack_t;
    Can anyone help me out?
    "node" is not a type. typedef like you did for "stack_t" or prefix with "struct ".

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Quote Originally Posted by zacs7 View Post
    "node" is not a type. typedef like you did for "stack_t" or prefix with "struct ".
    So the problem is in my definition of the node structure? I had a lot of trouble getting it to compile the node structure with a pointer to a node inside of it. What am I doing wrong?

    Edit:

    Code:
    typedef struct {
      node *next;  //compiler error on this line: expected specifier qualifier list before 'node'
      node *prev;
      char *data;
    } node;
    This gives me a compiler error. I am so confused
    Last edited by ChipsHandon; 02-25-2010 at 07:42 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    "node" is not a type. typedef like you did for "stack_t" or prefix with "struct ".
    Sure it is. @Chips: ignore zac

    This is the problem:
    Code:
    typedef struct node *node; //so I can use self referencing structure members
    [....]
    node *back;
    node is defined as pointer already, so node *back is equivalent to a pointer to a struct node pointer:
    Code:
    struct node **ptp;
    You have three options:

    1) change the typedef.
    2) dereference back appropriately:
    Code:
    return_val =  (*back)->data;
    3) don't declare back as a pointer to a pointer type:
    Code:
    node back;
    I'd try the third one first but give some thought to the first one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    typedef struct _node node;
    struct _node
    {
      node *next;  
      node *prev;
      char *data;
    };
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Quote Originally Posted by vart View Post
    Code:
    typedef struct _node node;
    struct _node
    {
      node *next;  
      node *prev;
      char *data;
    };
    Any identifier beginning with a single underscore is reserved for use by the implementation (for identifiers with file scope, IIRC).

    In other words, user code should not have identifiers that begin with a single underscore.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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