Problem with Unions in Program C

I am working on a C program that uses Union. The join definition is in the FILE_A header file and looks like this:

// FILE_A.h****************************************************
xdata union  
{
long position;
char bytes[4];
}CurrentPosition;

If I set CurrentPosition.position to FILE_A.c and then call a function in FILE_B.c that uses a join, the data in the join will return to zero. This is shown below.

// FILE_A.c****************************************************
int main.c(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB();
}

// FILE_B.c****************************************************
void SomeFunctionInFileB(void)
{
    // After the following lines execute I see all zeros in the flash memory.
    WriteByteToFlash(CurrentPosition.bytes[0];
    WriteByteToFlash(CurrentPosition.bytes[1];
    WriteByteToFlash(CurrentPosition.bytes[2];
    WriteByteToFlash(CurrentPosition.bytes[3];
}

Now, if I go to SomeFunctionInFileB (long pace) and then save it in CurrentPosition.bytes inside this function and finally call WriteBytesToFlash (CurrentPosition.bytes [n] ... it works fine.

It seems that Union CurrentPosition is not global. So I tried to change the join definition in the header file to include an extern keyword like this ...

extern xdata union  
{
long position;
char bytes[4];
}CurrentPosition;

(.c)...

xdata union  
{
    long position;
    char bytes[4];
}CurrentPosition;

:

C:\SiLabs\Optec Programs\AgosRot\MotionControl.c:76: error 91: extern definition for 'CurrentPosition' mismatches with declaration. C:\SiLabs\Optec Programs\AgosRot\/MotionControl.h:48: error 177: previously defined here

? ?

+3
4

FILE_A.h MotionControl.h? , , :

typedef
union xdata
{
    long position;
    char bytes[4];
} xdata;

(, ):

extern xdata CurrentPosition;   // in a header file

, C . , file_a.c:

xdata CurrentPosition;

, xdata, , flash SomeFunctionInFileB(), , , , , , . , , :

// in a header file
void SomeFunctionInFileB( xdata const* pPosition);


void SomeFunctionInFileB( xdata const* pPosition)
{
    // After the following lines execute I see all zeros in the flash memory.
    WriteByteToFlash(pPosition->bytes[0];
    WriteByteToFlash(pPosition->bytes[1];
    WriteByteToFlash(pPosition->bytes[2];
    WriteByteToFlash(pPosition->bytes[3];
}

:

int main.c(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB( &CurrentPosition);
}
+7

typedef extern FILE_A.h FILE_A.c.

-

// FILE_A.h

typedef union  
{
    long position;
    char bytes[4];
} Position;

extern Position CurrentPosition; // declaration

-

// FILE_A.c

#include "FILE_A.h"

Position CurrentPosition; // definition

int main(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB();
    return 0;
}

-

// FILE_B.c

#include "FILE_A.h"

void SomeFunctionInFileB(void)
{
    // now there will be valid data in the flash memory.
    WriteByteToFlash(cp.bytes[0];
    WriteByteToFlash(cp.bytes[1];
    WriteByteToFlash(cp.bytes[2];
    WriteByteToFlash(cp.bytes[3];
}

-

+2

You have not created an instance of the union.
You need:

// FILE_A.c****************************************************

#include "File_a.h"
CurrentPosition cp;
int main(void)
{
    cp.position = 12345;
    SomeFunctionInFileB();
}

// FILE_B.c****************************************************
#include "File_a.h"
extern CurrentPosition cp;
void SomeFunctionInFileB(void)
{
    // now there will be valid data in the flash memory.
    WriteByteToFlash(cp.bytes[0];
    WriteByteToFlash(cp.bytes[1];
    WriteByteToFlash(cp.bytes[2];
    WriteByteToFlash(cp.bytes[3];
}
+1
source

If sizeof(long)not 4, then endianess takes effect ...

consider

union{
   long position
   char bytes[sizeof long];
}
0
source

Source: https://habr.com/ru/post/1744042/


All Articles