I want to make a pointer to a struct that points to the next node of the struct dynamically allocated, usually we put a pointer '*pnext' of the same type of the struct inside the struct, in my case what I would like to do is to put the next pointer to a struct, outside of the struct itself, for instance in main, and point to that pointer with another pointer declared in the same function, how should I do it, and is that possible to do it?
Here's a glance of what it should be supposed to be like in case you didn't get my idea:
struct Node{
int data;
};
int main(){
struct Node *new_node;
struct Node *next;
new_node = malloc(sizeof(*new_node));
new_node->data=2;
new_node->next = malloc(sizeof(*new_node));
new_node->next->data = 3;
new_node->next = NULL; /* obviously I cannot do that it was just to make an example of
what I would like to do */
return 0;
}
There has to be a pointer in your structure to hold the address of next node. You cannot store it in main outside your structure.