c - 我如何为结构分配内存?

标签 c pointers linked-list

我有以下结构:

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));

关于c - 我如何为结构分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389219/

相关文章:

c - 关于C中的指针运算

c - 指向文件指针的指针,我该怎么做?

无法理解 sortedlinklist 函数

c - 链表头始终设置为 NULL

c++ - 有没有办法反编译 Linux .so?

c - 将 execle 与 wget 命令一起使用

c - 指向指针的指针

ruby - 在 Ruby 中实现链表

c - 分配某个变量后,双指针的地址被删除

c - 确定正则表达式是否只匹配固定长度的字符串