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