简体   繁体   English

带有typedef结构的extern const

[英]extern const with typedef struct

I have in .h file: 我有.h文件:

typedef struct MyName {

uint8_t AA;
uint8_t BB; 
};

extern const MyName NewName[num];

in file .c I have: 在文件.c我有:

const MyName NewName[num] = {{0x01,0x02}, {0x03,0x04}};

Compiler complains 'unknown type name' MyName. 编译器抱怨'未知类型名称'MyName。 How I can get around this? 我怎么能绕过这个? Thank you for helping. 谢谢你的帮忙。

The typedef should be typedef应该是

typedef struct myName 
{
    uint8_t AA;
    uint8_t BB; 
} 
MyName;

Without the final MyName , you haven't actually typedef'd anything. 如果没有最后的MyName ,你有没有实际typedef'd什么。

the correct way to write this is: 写这个的正确方法是:

in the .h file 在.h文件中

struct MyName 
{
   uint8_t AA;
   uint8_t BB; 
};

extern const struct MyName NewName[];
extern const int num;

in the .c file it should be: 在.c文件中它应该是:

const struct MyName NewName[] = {{0x01,0x02}, {0x03,0x04}};
const int num = sizeof( NewName ) / sizeof(struct MyName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM