C中的结构指针的大小是多少?

问题描述 投票:-2回答:3

如果结构指针的大小为4或说8个字节,它如何在动态内存分配中为其数据成员正确分配所需的内存。这是我的代码:

#include<stdio.h>
typedef struct node{
   char a[10];
   char b[10];
   char c[10];
   int f;
   struct node* next;
}node;

int main()
{
   node* temp;
   int d= sizeof(node *);
   printf("Size of structure pointer = %d",d);
   temp= (node*)malloc(sizeof(node*));
   int c = sizeof(temp);
   printf("Size of Temp = %d\n",c);
}
```````````````````
/*
Here both Temp and node* is having a size of 8 bytes. 
But the size of the structure 'node' is 38 bytes(total size of the members). 
So how can the pointer Temp allocate that much memory if its size is just 8 bytes ?
*/
c struct malloc dma
3个回答
1
投票

因为您没有将数据存储在指针中。指针指向其他地方分配的数据,因此称为“指针”。 sizeof(node*)是错误的,指针本身的大小无关紧要。

您应该做


0
投票

这是指向节点结构的指针,您无需为其分配空间。它可以指向节点结构。


0
投票

指针只是指针。如果您是战争博物馆的导游,并使用指针显示坦克,那么指针在您手中的重量是多少?木棍的重量还是水箱的重量?

© www.soinside.com 2019 - 2024. All rights reserved.