Array structur and extern declaration

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Array structur and extern declaration

1,862件の閲覧回数
kurna
Contributor I

Hi.

 

I have problem with extern declarition array off structure. I use S08JM.

In main.c I have declaration:

 

typedef struct {
    u8t   battery;
    u8t   sensor_connect;
    s16t  temperature;
    u16t  humidity;
} SENSORS_DATA_STRUCT;  
SENSORS_DATA_STRUCT    Sensors_data[10];

 

in second file.c I want to use extern declaration:

extern struct SENSORS_DATA_STRUCT Sensors_data[10];

 

but I can´t compile, extern declaration structure works, but array structure doesn´t work,

Do you know where is problen? thank you

ラベル(1)
0 件の賞賛
返信
2 返答(返信)

838件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

Is the typedef of the in the header file with the external declaration?

Also using SENSORS_DATA_STRUCT as struct tag (struct SENSORS_DATA_STRUCT) requires the struct to be defined with the same tag. E.g.

 

typedef struct SENSORS_DATA_STRUCT {
    u8t   battery;
    u8t   sensor_connect;
    s16t  temperature;
    u16t  humidity;
} SENSORS_DATA_STRUCT;  

 

basically in C tag names are in their own namespaces, the code snippet was using the SENSORS_DATA_STRUCT identifier as tag, but did not the identifier as tag too.

 

To avoid duplication you can also just use the tag only, then there is no need for a typedef.

The type definition then looks like this:

 

struct SENSORS_DATA_STRUCT {
    u8t   battery;
    u8t   sensor_connect;
    s16t  temperature;
    u16t  humidity;
};

 

tags can also be declared only as "struct SENSORS_DATA_STRUCT;" but you will need the type definition to actually define or access the array.

 

Just using the typedef name is also possible as long as you do not need any forward declaration or any recursive references, that only works with tags.

 

If this does not help in your case, please provide the full setup (compilable code) and the exact error message you get from the compiler.

 

Daniel

 

PS: Note that C++ has different rules in respect to tag names, in C++ tag identifiers get introduced in the enclosing namespace.

 

 

0 件の賞賛
返信

838件の閲覧回数
Lundin
Senior Contributor IV

The problem is that any declaration in main.c  isn't visible to xxx.c. Simply put the original typedef struct into a h-file, problem solved. Though the array declaration should of course still be in a c-file.

 

0 件の賞賛
返信