C 程序中的 Union 问题

发布于 2024-08-31 11:02:45 字数 1536 浏览 4 评论 0原文

我正在开发一个使用 Union 的 C 程序。联合定义位于 FILE_A 头文件中,如下所示...

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

如果我在 FILE_A.c 中设置 CurrentPosition.position 的值,然后调用 FILE_B.c 中使用联合的函数,联合中的数据将返回到零。下面对此进行了演示。

// 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];
}

现在,如果我将 long 传递给 SomeFunctionInFileB(long temp) ,然后将其存储到该函数内的 CurrentPosition.bytes 中,最后调用 WriteBytesToFlash(CurrentPosition.bytes[n]... 它工作得很好。

看起来好像 CurrentPosition Union 不是全局的,所以我尝试更改头文件中的 union 定义以包含这样的 extern 关键字...

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:错误 91:“CurrentPosition”的外部定义与声明不匹配。 C:\SiLabs\Optec Programs\AgosRot\/MotionControl.h:48: 错误 177: 先前在此处定义

那么我做错了什么?如何使工会全球化?

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

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

If I set the value of CurrentPosition.position in FILE_A.c and then call a function in FILE_B.c that uses the union, the data in the union is back to Zero. This is demonstrated 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 pass a long to SomeFunctionInFileB(long temp) and then store it into CurrentPosition.bytes within that function, and finally call WriteBytesToFlash(CurrentPosition.bytes[n]... it works just fine.

It appears as though the CurrentPosition Union is not global. So I tried changing the union definition in the header file to include the extern keyword like this...

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

and then putting this in the source (.c) file...

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

but this causes a compile error that says:

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

So what am I doing wrong? How do I make the union global?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心是晴朗的。 2024-09-07 11:02:45

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 变量传递给 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);
}

Is FILE_A.h really MotionControl.h? If so I think the fix is to define a union type in the header:

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

And declare a global variable of that type elsewhere in a header file (maybe the same one):

extern xdata CurrentPosition;   // in a header file

Finally define the global variable in a C file exactly once. Maybe in file_a.c:

xdata CurrentPosition;

Of course a better fix might be to pass the xdata variable you want to write out to flash to SomeFunctionInFileB() so you don't have to depend on a global variable, which are well known to be problematic when not very, very carefully used. And there seems to be no good reason to not pass the data as a parameter:

// 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];
}

And call it like so:

int main.c(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB( &CurrentPosition);
}
分开我的手 2024-09-07 11:02:45

理想情况下,您需要联合的 typedef 和 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];
}

-

Ideally you need a typedef for the union and an extern declaration in FILE_A.h and the actual definition of the union in 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];
}

-

找个人就嫁了吧 2024-09-07 11:02:45

您尚未实例化联合。
你需要:

// 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];
}

You haven't instantiated 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];
}
暮年慕年 2024-09-07 11:02:45

如果 sizeof(long) 不是 4,那么字节序就会发挥作用......

考虑

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

If sizeof(long) is not 4, then endianess comes into play...

consider

union{
   long position
   char bytes[sizeof long];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文