小编LEA*_*NER的帖子

malloc(sizeof(ptr)) 和 malloc(sizeof(ptr*)) 的区别?

我想知道这两行有什么区别:

 queue* f=(queue*)malloc(sizeof(queue));
Run Code Online (Sandbox Code Playgroud)
 queue* f=(queue*)malloc(sizeof(queue*));
Run Code Online (Sandbox Code Playgroud)

队列的定义如下:

typedef struct queue
{
    int arr[N];
    int tail;
}queue;
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c pointers dynamic-memory-allocation

1
推荐指数
1
解决办法
72
查看次数

sizeof(p) 和 sizeof(*p) 之间不同,而 p 具有矩阵的基本类型

我正在学习指针,由于以下程序给出的输出,我有点困惑:

#include <stdio.h>   
#define R 10 
#define C 20 
int main() 
{ 
   int arr[R][C];
   int (*p)[R][C]=&arr; 
   printf("%d ",  sizeof(p)); 
   printf("%d ",  sizeof(*p));  
   return 0; 
}
Run Code Online (Sandbox Code Playgroud)

输出 : 4 800

为什么输出是800?我们知道这p是一个具有二维10行和20列数组的基本类型的指针,这意味着*p指向矩阵0th array的 ,这意味着sizeof(*p)=20*sizeof(int)=80并且sizeof(p)应该等同于800但输出与我的计算不同!

我能得到解释吗?非常感激。

c pointers sizeof multidimensional-array

1
推荐指数
1
解决办法
62
查看次数