【问题标题】:What is the sizeof the pointer pointing to a structure variable?指向结构变量的指针的大小是多少?
【发布时间】: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 = &amp;b;。您正在将array of structure vars 的地址分配给pointer to structure var。只需p = b;

标签: c pointers output structure


【解决方案1】:

首先,sizeof 运算符产生一个类型size_t,您必须使用%zu 来打印它。

然后,通常在任何体系结构中,指针的大小始终是恒定的,与它们指向的类型无关。换句话说,指针需要保存一个内存位置,并且对于任何正常的架构,(内存位置的)地址具有固定大小。所以,任何指针的大小,都是一样的。

【讨论】:

  • 指针大小总是一样的,除了函数指针,它可能更大,对吧?
  • @FiddlingBits 好吧,没有规范,但是是的,这是实际的事情。添加了一行来澄清。
  • @FiddlingBits with the exception of function pointers 是什么?我到处读到sizeof(pointers) 在任何机器上总是相同的,不管它指向什么数据类型。即使在阅读了您的评论后,我检查了它并发现(在我的系统上)sizeof a function pointer to intsizeof a pointer to int 是相同的。
  • @FiddlingBits 指针大小总是一样的,... 没有。Are there any platforms where pointers to different types have different sizes?
  • @Lucas:对于 C,唯一的要求是 char *void * 具有相同的大小和对齐方式,指向合格类型的指针具有与指向它们的指针相同的大小和对齐方式非限定等价物,所有指向struct 类型的指针具有相同的大小和对齐方式,并且指向所有union 类型的指针具有相同的大小和对齐方式。虽然不同的指针类型在 x86 等硬件上可能具有相同的大小,但这并不能保证并且不应依赖。
【解决方案2】:

这是你想要的,在第二种情况下:

printf("%zu %zu\n", sizeof(p1), sizeof(*p1));
// Output
8 160

好的!让我们从头开始。

正如 Sourav Ghosh 在他的回答中所说,pointer 需要保存一个内存位置,对于任何正常的架构,(内存位置的)地址都有一个固定的大小。因此,任何指针的大小都是相同的。”,无论它指向的数据类型如何。

现在来解决您的问题,考虑并尝试理解您的程序的这个修改版本:

#include <stdio.h>

struct Book
{
    char name[10];
    int price;
};

int main(void)
{
    struct Book b[10];  // Array of structure variables
    struct Book* p;     // Pointer to type struct Book
    struct Book (*p1)[10]; // Pointer to type struct Book[], which is array of type struct Book

    p = b; // same as p = &b[0] but not as p = &b
    p1 = &b; // this is how assignment of a pointer to array is done

    printf("%zu %zu\n", sizeof(struct Book*), sizeof(struct Book));
    printf("%zu %zu\n",sizeof(p),sizeof(*p));
    printf("%zu %zu\n",sizeof(p1),sizeof(*p1));

    return 0;
}

输出:

// perhaps on your PC
8 16
8 16
8 160
// on my PC
4 16
4 16
4 160

你在输出中看到sizeof(struct Book), sizeof(p), and sizeof(p1),都是一样的。因此,任何类型的指针的大小都是相同的。

但是当您打印struct Book 的大小时,即您在询问编译器时,“告诉我这个结构 Book 包含多少字节的内存”

对于前两种情况(sizeof(struct Book) or sizeof(*p)),它是 16,对于最后一种情况,它是 160,这是 10 个 struct Book 类型变量的大小。

如果您想知道为什么16stuct Book 类型变量的大小,那是因为2 padding bytes 位于charint 成员之间。

阅读此SO question about padding and packing in structures

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2014-07-08
    • 2021-10-15
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多