0

Sorry if the title is a little confusing, as a beginner to C I really didn't know how else to put it. So I'm trying to create a singly linkedlist where each node not only has a pointer to the next node, but also a pointer to sub-linkedlist that starts from that node. Basically each node has its own linkedlist. In trying to create this LL, I've got a struct that houses characteristics of what each node should have, such as a pointer to the next node and a pointer to the linkedlist of data that stems from it. I've heard that you're actually able to make a struct of a struct. I want to make a second struct that structures the types of data that the node is holding:

struct NODE{
    void* data;
    struct Node* next;
}NODE;

typedef struct stuff{
    int data1;
    int data2;
    int data3;
}STUFF;

My question now, is that say I have a node from struct NODE like "NODE* node", how would I reference this node's types of data (data1, data2, etc)? I've tried casting, but not sure if I'm doing it wrong but something like this:

(STUFF)node->data1= DATA1; 
//where DATA1 is of course already defined to be some int

I did this inside of a function, and I get the error "Assignment to cast is illegal, lvalue casts are not supported".

6
  • It's not clear what you mean by this "sub-linked-list". You seem to be describing something of type struct stuff, except that does not look like a list node. Are you trying to pack this structure inside a void*? Please construct an example list and its contents to illustrate what you're trying to do. It sounds like you want a linked list where the data member of each node points to another struct NODE (as the sub-list) and each of those nodes have their data member point to a struct stuff value. Is that correct? Commented Nov 29, 2021 at 3:24
  • what is the difference between NODE and Node types? Where is Node defined? Commented Nov 29, 2021 at 3:26
  • Basically I wanted to create a singly linkedlist where, compared to a basic LL each node only has just 1 piece of data, this LL has nodes that have multiple pieces of data (data1, data2, etc.) Commented Nov 29, 2021 at 3:28
  • then why dont you use a pointer of type STUFF instead of void? Commented Nov 29, 2021 at 3:28
  • 1
    Are you a classmate of @uwu? See this question. Commented Nov 29, 2021 at 3:30

1 Answer 1

1

Concerning your cast problem... Here is how you could do it.

typedef struct Node{
    void* data;
    struct Node* next;
}NODE;

typedef struct stuff{
    int data1;
    int data2;
    int data3;
}STUFF;

int main()
{
    NODE *n = malloc(sizeof(NODE));
    n->data = malloc(sizeof(STUFF));
    STUFF *s = n->data;
    s->data1 = 15;
    printf("%d", ((STUFF*) n->data)->data1);
    return 0;
}
Sign up to request clarification or add additional context in comments.

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.