如何检查结构成员是否存在于C中?

art*_*pro 2 c struct c99

如何检查我的结构something在 C99 中是否有成员?

#include <stdlib.h>
#include <string.h>

struct some {
  char title[50];
  char name[50];
};

int main() {
  struct some s;

  if (*s.something) { // Error: no member named 'something' in 'struct.some'
    strcpy(s.something, "Hello");
  }
}
Run Code Online (Sandbox Code Playgroud)

更新:

我不需要知道它在编译时是否存在,而是在构建的程序中。成员及其值将从文件中解析,然后在循环中消耗到结构中,但我需要确保它会跳过所有不存在的成员。

Ste*_*ner 5

C99(甚至 C++)不支持反射。因此无法在运行时检查结构是否包含具有特定名称的成员;编译器会在编译时告诉你。

这与其他语言不同,例如支持反射的 Java。