[英]Why am I getting a type error when trying to use a struct from a header in my .c file?
I really don't understand what is happening here. 我真的不明白这里发生了什么。 I'm trying to access members of a struct in a .c file, but it's giving an 'error-type' when I try to access the struct variable.
我正在尝试访问.c文件中的结构成员,但是当我尝试访问struct变量时,它给出了“错误类型”。 Anybody have any idea what's going on here?
有人知道这是怎么回事吗?
#ifndef _CPU_H
#define _CPU_H
#include <stdint.h>
typedef struct cpu_registers
{
union
{
struct
{
uint8_t f;
uint8_t a;
};
uint16_t af;
};
union
{
struct
{
uint8_t c;
uint8_t b;
};
uint16_t bc;
};
} cpu_registers;
#endif /* _CPU_H */
#include "CPU.h"
cpu_registers regs;
regs.af = 0xFFFF;
Here are the errors upon compilation with clang: 这是使用clang编译时的错误:
CPU.c:4:1: error: unknown type name 'regs'
regs.af = 0xFFFF;
^
CPU.c:4:5: error: expected identifier or '('
regs.af = 0xFFFF;
^
2 errors generated.
You can declare and initialize global variables outside of functions, but you cannot do anything else with them. 您可以在函数外部声明和初始化全局变量,但不能对它们执行任何其他操作。
So, you could do this: 因此,您可以这样做:
cpu_registers regs = { .af = 0xFFFF };
However, do note that this will not work: 但是,请注意,这将不起作用:
int val = 0xFFFF;
cpu_registers regs = { .af = val };
And - maybe a bit surprisingly - not this either: 而且-也许有点令人惊讶-也不是:
const int val = 0xFFFF;
cpu_registers regs = { .af = val };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.