【发布时间】:2020-07-06 15:05:55
【问题描述】:
我试图在这两种情况下打印指针的大小。对于这两种情况,我都会得到8 16 作为输出。
#include <stdio.h>
struct Book
{
char name[10];
int price;
};
int main(void)
{
struct Book a; // Single structure variable
struct Book* ptr; // Pointer of Structure type
ptr = &a;
struct Book b[10]; // Array of structure variables
struct Book* p; // Pointer of Structure type
p = &b;
printf("%ld %ld\n",sizeof(ptr),sizeof(*ptr));
printf("%ld %ld\n",sizeof(p),sizeof(*p));
return 0;
}
【问题讨论】:
-
Ajay,您希望这些分隔线在您的帖子末尾表示什么?请消除这种干扰。尤其是如果您插入它们只是为了绕过系统告诉 oyur 您的问题中有太多代码而没有随附的散文解释。
-
除了所有的嗡嗡声之外,您的代码中还有一个不兼容的赋值操作,即
p = &b;。您正在将array of structure vars的地址分配给pointer to structure var。只需p = b;。
标签: c pointers output structure