I have the following struct:
struct Node{
int *VC;
Node *Next;
};
My goal is to create a linked list of pointers pointing to an int
My question is how can i allocate memory for Node
. ie
int* ptr = (int *) malloc(sizeof(int)*10);
//code to allocate memory for a new Node n
n->VC = ptr;
n->Next = null;
then later i may do:
int *_ptr= (int *) malloc(sizeof(int)*10);
//code to allocate memory for a new Node c
c->VC= _ptr;
c->Next = null;
n->Next = c;
Allocating memory for a struct
is the same as allocating memory for an int
(in C). Just use sizeof
to get the size of the struct:
struct Node *n = malloc(sizeof(struct Node));
Node *c = malloc(sizeof(*c));
\n
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.