0
#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int data;
    struct Node* next;
}Node;

Node* head;

void AddEntry(){
    int x;
    Node* temp;
    temp = head;
    while(temp != NULL){
        temp = temp->next;
    }
    Node* temp1 = (Node*)malloc(sizeof(Node));
    temp->next = temp1;
    printf("What is the value for this entry?\n");
    scanf("%d",&x);
    temp1->data = x;
    temp1->next = NULL;
}
void PrintList(){
    Node* temp;
}



int main(void){

}

When I compile this code I get the compiler error:

pointertest.c: In function ‘AddEntry’:

pointertest.c:16:8: warning: assignment from incompatible pointer type [enabled by default]
   temp = temp->next;
        ^
pointertest.c:19:13: warning: assignment from incompatible pointer type [enabled by default]
  temp->next = temp1;

I am not understanding why that is. I’ve seen this done in my textbook and elsewhere. I thought it was assigning the pointer temp to address saved in temp next.

Thanks for your help

2 Answers 2

3

Your structure definition is bogus. Try this instead:

typedef struct tagNode {
  int data;
  struct tagNode *next;
} Node;

In your code, there is no such thing as a struct Node, there is just an unnamed structure with an "alias" (typedef) called Node.

By defining it this way, you can declare of variable of this type by doing either:

struct tagNode foo;

Or:

Node foo;

But when I typedef a struct like this, I avoid using the tag to avoid confusion.

It has been brought up in comments that it might be confusing why I chose to use struct tagNode instead of struct Node. Both work equally well, but my personal preference is to use different names to avoid confusion later on. I find it easier to visually disambiguate struct tagNode *foo from Node *foo then if I had just used struct Node.

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

7 Comments

So that extra term tagNode is used when you need to reference the struct inside the declaration?
Yes. There are other ways to do the same thing, but that is one way of doing it.
I was always wondering why that was only sometimes included. Makes perfect sense, thanks so much!
There's no compelling reason to use a different name for the typedef. This answer is potentially misleading as it implies a different name is needed for the incomplete type reference inside the struct definition.
I don't think that can cause confusion, what does cause confusion is typedefing struct Node *.
|
3

There is no struct Node in your code.

This is a typedef for an anonymous structure

typedef struct {
    int data;
    struct Node* next;
} Node;

which is valid in c, but up to here there is no struct Node declared at all, you need

typedef struct Node {
    int data;
    struct Node* next;
} Node;

If you are going to typedef it anyway, you can do it like this too

typedef struct Node Node;
struct Node
 {
    int   data;
    Node *next;
 };

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.