My question is how would I access the num variable in a node struct of a list struct? I tried two ways and both of them didnt work? Im just curious to why that is. Thank you to anyone who helps I know this is a novice question. Im fairly new to c and stack overflow, hopefully I can learn much from this website.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *next;
} node;
typedef struct list
{
node *ptr;
struct list *next;
} list;
int main()
{
list *p = malloc(sizeof(list));
//p->ptr->num = 5;
node *x;
x = p->ptr;
//x->num = 5;
return 0;
}
nodeforxto point to, then setpto point to the node. Only then can you start accessing the value. So,p->ptr = x;is more plausible than the reverse assignment, but you still have to connect all the dots.