This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Two question in one, typedef union {} and funtion of integer

Hello,

Being here just 30 seconds.

I have two questions, where the first one is about using a typdef union{} in other files, see my code here

// a.h
typedef union{
   float vf;
   unsigned char vuc[4];
}fc4;

// a.c
fc4 vfc4a;

// b.c
#include "a.h"
extern fc4 vfc4a;

Compiler gives an error telling that fc4 undefined, see it 

error:  #20: identifier "fc4" is undefined

The second one is about using an integer function in other file. It is actually another attempt of the one above.

 I failed to use the typedef union in other file. Instead, I define a variable of float in file a.c and redeclare it as extern in a global .h file which was included in file b.c, see this.

// a.c
fc4 vfc4a;
float vfa;

int Parsing(void)
{ 
   for(int i=0; i<4; i++)
   vfc4a.vuc[i] = RxBuf[i]; // suppose there is a valid RxBuf from the serial

vfa = vfc4a.df;
printf("%s %f\n", "In parsing()", vfa);

return 1;
}

// b.c
#include "global.h"    // extern float vfa in this.
void a_function(void)
{ 
    int ret = Parsing();
    if(ret)
       printf("%s %f\n", "In b.c", vfa);
    for(int i=0; i<4; i++)
       printf("%x ", vfc4a.vuc[i]);
    printf("\n");
}

printf() is redirected to a serial port sending data to a PC. The PC can receive output given by printf in a_function but cannot receive the one in Parsing(). Besides, vfa in a_function is always 0 including the case that RxBuf is not 0.

Parents Reply Children
No data