c - 之前预期的错误说明符限定符列表

标签 c

我试着编译这个例子:

#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/

相关文章:

c - C 语言编程。数据验证和将字符转换为整数

c - 在结构体中使用 char

c - 最长可移植无符号整数类型

c - 给定一个程序,我们如何确定执行的保存/恢复操作的数量?

c - 从 Windows 10 内核模式驱动程序记录到 txt 文件

c - 将字符串构建为链表

将结构体中的 int 转换为 C 中的字符串

c - 在 C 中使用逻辑运算符切换大小写

c - 带有 IDAT 的调色板基础 PNG,其 BTYPE=00 用于无压缩,现在带有 Adler32 代码

c - VS2012项目+openssl(LNK2001)