相关疑难解决方法(0)

是否允许不同的翻译单元定义具有相同名称的结构?

假设我有a.cb.c,它们都定义了被调用的类型struct foo,具有不同的定义:

#include <stdio.h>

struct foo {
    int a;
};

int a_func(void) {
    struct foo f;
    f.a = 4;
    printf("%d\n", f.a);
    return f.a * 3;
}
Run Code Online (Sandbox Code Playgroud)

 

#include <stdio.h>

struct foo { // same name, different members
    char *p1;
    char *p2;
};

void b_func(void) {
    struct foo f;
    f.p1 = "hello";
    f.p2 = "world";
    printf("%s %s\n", f.p1, f.p2);
}
Run Code Online (Sandbox Code Playgroud)

在C中,这些文件是否可以作为符合标准的程序的一部分链接在一起?

(在C++中,我认为One Definition Rule禁止这样做.)

c struct one-definition-rule

9
推荐指数
1
解决办法
773
查看次数

标签 统计

c ×1

one-definition-rule ×1

struct ×1