0
typedef struct class
{
   ...
}Class;

typedef struct node
{
    Class data;
    struct node *next;
}Node;

Node* newNode()
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp -> data = malloc(sizeof(Class));
    temp -> next = NULL;
    return temp;
}

The compiler says there is a problem with the line: temp -> data = malloc(sizeof(Class));, specifically "incompatible types in assignment".

What am I doing wrong?

1 Answer 1

1

You're saying that data is a Class but you're trying to assign it a Class*.

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

7 Comments

Okay, I see the problem, so how would I fix it?
In the struct definition, change Class data; to Class* data;
@sdfrn42, better still might be to just do nothing. Your struct node has a Class as a member, so the space allocated for a node already includes space for a Class. You don't need to allocate that separately. Change the member to a Class * only if that's what you actually want to have.
Will do in a couple mins when it lets me.
@JohnBollinger I didn't actually think of that... What exactly changes when i change it to Class *?
|

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.