我试着编译这个例子:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
main(){
size_t distance;
struct x{
int a, b, c;
} s_tr;
distance = offsetof(s_tr, c);
printf("Offset of x.c is %lu bytes\n", (unsigned long)distance);
exit(EXIT_SUCCESS);
}
我收到一个错误:'s_tr' 之前的错误预期说明符限定符列表。这是什么意思?我从中得到的例子:http://publications.gbdirect.co.uk/c_book/chapter9/introduction.html
最佳答案
第二次阅读时,似乎有人不小心在 {
之前插入了一个 x
。原件可能有一个匿名结构:
struct { int a, b, c; } s_tr;
您可以使用 typedef 重写它:
typedef struct { int a, b, c; } newtype;
newtype s_tr;
这是我用来重温内存的一些代码,其中包含在 C 中声明结构的各种方法(匿名、标记和类型化):
// Anonymous struct
struct { int a,b; } mystruct1;
// Tagged struct
struct tag1 { int a,b; };
struct tag1 mystruct2; // The "struct" is not optional
// Typedef declaring both tag and new type
typedef struct tag2 { int a, b; } type1;
struct tag2 mystruct3;
type1 mystruct4; // Unlike tags, types can be used without "struct"
关于c - 之前预期的错误说明符限定符列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1695802/