4

I'm using this structure as a linked list:

 typedef struct Node{
       int value;
       struct node_t* next;

 }node_t;

And everything works fine until I put struct node_t* next before int value field, then I have a lot of trash values working with that structure. Is it about wrong implementation or something else in the code?

2
  • 1
    In your code node_t is an alias for struct Node now. Hope that helps to understand. Commented Jul 22, 2016 at 8:54
  • Please read the updated answer, do you understand now? Commented Jul 22, 2016 at 12:46

3 Answers 3

6

You are calling your structure Node and defining a node_t type. Then you are using node_t as if it was the name of the structure and not the type.

Try this

typedef struct node {
    int value;
    struct node *next;
} Node;

Or

typedef struct node Node;
struct node {
    int value;
    Node *node;
};

If you call it struct Node, then

struct Node {
    int value;
    /* The compiler doesn't know what `struct Node' is yet */
    struct Node *next;
    /* But you can always declare pointers, even of types the compiler
     * doesn't know everything about. Because the size of a pointer
     * does not depend on the type of the pointee.
     */
};

In your example, it's even worse. You typedefed something that is a new type as the compiler understand it, to use it you MUST not use struct. The whole idea behind typedefing is that you DEFINED a new type, so suppose the following

typedef struct Node node;

then to declare a pointer of type node (note, again node IS A TYPE),

node *anode;

but you attempted something like

struct node *anode;

and it's wrong because there is no struct node in the code above, it's struct Node.

Another mistake in your code is that, the node_t type does not exist when the compiler finds the

struct node_t *next;

which is already wrong because if the type were defined before the struct which is possible like this

typedef struct Node node_t

it'd still be wrong to use struct on the node_t type, because for the compiler node_t is not a struct it's a new type, which in turn is simply an alias for struct Node.

Typedefing structures in my experience is more trouble than benefit anyway. And it's not so hard to type struct Something instead of just Something. It also has the benefit of being more explicit, so if another programmer reads your code, they will immediately know that Something is a struct.

Note: I deliberately changed the name to node because it's considered bad practice to suffix your own defined types with _t. It's not necessarily a bad thing, but over the years that I've been working with this I developed some habits and one of them is not to use _t as a suffix for my own defined types. Which by the way only exist in my code if they will improve readability a lot. Otherwise I simply use the name of the structure with the struct keyword.

Sign up to request clarification or add additional context in comments.

1 Comment

@ead What you mean? If you think about the way you did it, it does not make sense at all. You are using a different tag for the struct in two different places, it DOESN'T make sense. Consistency is very important and even if your code would make sense and would compile correctly, it's inconsetent with itself so it's bad practice. You should read about the syntax deeply to understand what is valid and what invalid syntax. I have been programming in c for 5 years or so, and I still don't know ALL the syntax. I know that I don't because frequently I find something that I didn't know before.
1

You are using a non existing type node_t. The type doesn't exist because the type struct Node is not even complete and you are using it's alias. Another thing to remember while using typedefs with structures don't use struct keyword along with alias eg.

/* This is correct */
typedef struct Node
{
    int x;
    struct Node *next;
} node_t;

/* while these are incorrect */

/* Prefixing struct keyword to a typedef'ed type */
struct node_t *listhead;

/* The type is inclomplete and you are using an alias of the type
   which doesn't even exist */
typedef struct Node
{
    int x;
    node_t *next;
};

Comments

1

You are trying to create a pointer to the structure which you are yet to create. So, it should have been,

typedef struct Node{
int value;
struct Node* next;
}node_t;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.