Array structur and extern declaration

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Array structur and extern declaration

1,752 Views
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

Labels (1)
0 Kudos
Reply
2 Replies

728 Views
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 Kudos
Reply

728 Views
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 Kudos
Reply