【问题标题】:How to get size of inner structure如何获得内部结构的大小
【发布时间】:2019-04-19 13:31:51
【问题描述】:

我正在尝试获取内部结构的大小,即struct B。但我收到编译错误:

prog.c:在函数“main”中: prog.c:10:53: 错误: ':' 标记之前的预期')' printf("%d | %d", sizeof(struct A), sizeof(struct A::struct B));

以下是我的代码:

#include <stdio.h>

struct A
{
        struct B{};
};

int main() {
    printf("%d | %d", sizeof(struct A), sizeof(struct A::struct B));
    return 0;
}

您能否建议我如何在 C 中实现这一点?

更新

@Jabberwocky 的回答解决了我的上述问题。但是下面的代码呢。这个也可以找到here

#include <stdio.h>

struct A
{
    struct B{};
};

struct B
{};

int main() {
    printf("%d | %d", sizeof(struct A), sizeof(struct B), sizeof(struct A::struct B));
    return 0;
}

在这种情况下,我收到如下编译错误:

prog.c:8:8: 错误:“结构 B”的重新定义
结构 B
^
prog.c:5:10: 注意:最初定义在这里
结构 B{};
^
prog.c:在函数“main”中:
prog.c:12:71: 错误: ':' 标记之前的预期')'
printf("%d | %d", sizeof(struct A), sizeof(struct B), sizeof(struct A::struct B));

这里我如何区分struct Bstruct A::struct B

【问题讨论】:

  • 对于您问题的第二部分,请阅读下面pmg的答案。简短的回答:你不能。
  • 没有struct A::struct B。 C 不将:: 识别为有效令牌;也许你在考虑 C++?
  • @pmg 我明白这一点,因为那是我得到编译错误的地方。这只是为了表明我想做什么?。谢谢!!!为您的评论和回答:)
  • C 也不能煎鸡蛋......好吧......如果你在绝缘不佳的计算机上使用繁忙的循环:-)
  • 请不要编辑问题来提出新问题。当您有新问题时,请输入一个单独的新问题。 Stack Overflow 不是为您提供的个人服务;它旨在创建一个持久的问题和答案存储库。当未来的读者发现您的多题问题时,他们可能很难弄清楚哪些答案是为您的第一个问题编写的,哪些是为您的第二个问题编写的。

标签: c sizeof


【解决方案1】:
#include <stdio.h>

struct A
{
        struct B{};   // this should not compile anyway in C as C
                      // does not allow empty structs
                      // but this would compile: struct B{int a;};
};

int main() {
    printf("%d | %d", sizeof(struct A), sizeof(struct B));
                                           // just write struct B
    return 0;
}

工作样本:

#include <stdio.h>

struct A
{
  int b;
  struct B { int a; };
};

int main() {
  printf("%d | %d", sizeof(struct A), sizeof(struct B));
  return 0;
}

32 位系统上的可能输出:

8 | 4

【讨论】:

  • this should not compile anyway in C 据我所知,C 确实支持嵌套结构?
  • @DeiDei 是的,但 C 不允许 empty 结构,struct B{} 显然是空的。
  • @cse 一个符合标准的 C 编译器不会编译这个:看here。但在 C++ 中它可以编译。这是一个 gnu C 扩展。阅读:stackoverflow.com/a/24685483/898348
  • @cse 空结构在标准 C 中是不允许的,但 gnu c (gcc) 支持它们作为扩展。
  • @cse -- "要验证 C 编译器是否支持内部结构,.... ":您无法通过在 C 中运行代码来验证编译器是否支持某些东西. 您需要求助于文档,因为未定义的行为可能包括工作代码的外观。在这种情况下,虽然 C 支持 嵌套 结构,但 empty structures are not, leading to undefined behavior.
【解决方案2】:

请注意,C 对嵌套结构的支持只是合乎逻辑的。每个结构都独立存在。

#include <stdio.h>
struct A {                   // same as
    int bar;                 // struct B { int foo };
    struct B { int foo; } b; // struct A { int bar; struct B b; };
};

int main(void) {
    struct A a;
    a.bar = 42;
    a.b.foo = -1;
    printf("a.bar is %d; a.b.foo is %d\n", a.bar, a.b.foo);

    struct B b; /* struct B is visible outside struct A */
    b.foo = 666;
    printf("b.foo - a.bar is %d\n", b.foo - a.bar);
    return 0;
}

https://ideone.com/rHaanj

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2021-08-09
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多