cancel
Showing results for 
Search instead for 
Did you mean: 

Function of integer calling in another file skipped partly

Zy
Associate

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.

2 REPLIES 2

Hello

For the first question, inside a.c define vfc4a as volatile. (volatile fc4 vfc4a;)

The vfc4a never used (by the mean of change its value), compiler optimized the code by eliminate the definition.

For the second question, examine if printf returns before last byte sent to PC and not overwrites the previous operation in progress.

@Vangelis Fortounas​ Thanks for giving answer. I'll try.