簡體   English   中英

我如何為結構分配內存?

[英]How can i Allocate Memory for a struct?

我有以下結構:

struct Node{
    int *VC;
    Node *Next;
};

我的目標是創建一個指向int的指針的鏈接列表

我的問題是如何為Node分配內存。

int* ptr = (int *) malloc(sizeof(int)*10);
//code to allocate memory for a new Node n
n->VC = ptr;
n->Next = null;   

然后稍后我可能會做:

 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;

struct分配內存與為int分配內存(在C中)相同。 只需使用sizeof即可獲得結構的大小:

struct Node *n = malloc(sizeof(struct Node));
Node *c = malloc(sizeof(*c));
\n

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM