帮助将指针转换为联合

发布于 2024-09-26 03:50:00 字数 893 浏览 2 评论 0原文

我正在研究 C 固件项目。我有一个联合,定义为,

typedef union {
    unsigned long value;
    unsigned char bytes[4];
} LONGVALUE;

我还有一个具有此原型的函数,

char sendHexToASCII_UART(char *msg, int cnt);

以及一个 LONGVALUE 类型的变量定义为,

LONGVALUE countAddr;

问题: 我用数组 (tempBuff1) 中的值填充联合变量的各个字节,然后我想将联合中第一个元素的地址传递给一个函数,该函数将其打印到 UART。在我的函数调用 sendHexToASCII_UART((int *)countAddr.bytes[0], 4); 上,我收到一条编译器警告,提示“从不同大小的整数转换为指针”。有人可以解释一下为什么我会出现这种情况以及如何让它消失吗?注意:将 (int *) 转换更改为 (char *) 会导致相同的警告。

countAddr.bytes[0] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS];
countAddr.bytes[1] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS + 1];
countAddr.bytes[2] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS + 2];
countAddr.bytes[3] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS + 3];
sendHexToASCII_UART((int *)countAddr.bytes[0], 4); 

I am working on C firmware project. I have a union that is defined as,

typedef union {
    unsigned long value;
    unsigned char bytes[4];
} LONGVALUE;

I also have a function that has this prototype,

char sendHexToASCII_UART(char *msg, int cnt);

and a variable of type LONGVALUE defined as,

LONGVALUE countAddr;

THE PROBLEM:
I fill the individual bytes of the union variable with values from an array (tempBuff1), I then want to pass the address of the first element in the union to a function that will print it to the UART. On my function call sendHexToASCII_UART((int *)countAddr.bytes[0], 4);, I get a compiler warning saying "cast to pointer from integer of different size". Can someone explain why I am getting this and how I can make it go away? NOTE: Changing the (int *) cast to (char *) causes the same warning.

countAddr.bytes[0] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS];
countAddr.bytes[1] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS + 1];
countAddr.bytes[2] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS + 2];
countAddr.bytes[3] = tempBuff1[COUNT_ADDR - PWRD_ADDRESS + 3];
sendHexToASCII_UART((int *)countAddr.bytes[0], 4); 

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

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

发布评论

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

评论(4

波浪屿的海角声 2024-10-03 03:50:00

为什么不只是:

sendHexToASCII_UART((char *)countAddr.bytes, 4);

甚至更好:

sendHexToASCII_UART((char *)countAddr.bytes, sizeof(countAddr.bytes));

Why not just:

sendHexToASCII_UART((char *)countAddr.bytes, 4);

or even better:

sendHexToASCII_UART((char *)countAddr.bytes, sizeof(countAddr.bytes));
扛起拖把扫天下 2024-10-03 03:50:00

sendHexToASCII_UART((int *)(&countAddr.bytes[0]), 4);

或:

sendHexToASCII_UART((int *)(countAddr.bytes), 4);

sendHexToASCII_UART((int *)(&countAddr.bytes[0]), 4);

or:

sendHexToASCII_UART((int *)(countAddr.bytes), 4);

冷清清 2024-10-03 03:50:00

表达式的值

countAddr.bytes[0]

是一个无符号字符(数组中的第一个字节),您将其转换为 int 指针,因此会出现警告。正如 Oli 所说,您必须删除 [0] 才能获得指向数组的指针,这就是您想要的。

The value of the expression

countAddr.bytes[0]

is an unsigned char (the first byte in your array), which you cast to an int pointer, hence the warning. As Oli said, you must remove the [0] to get a pointer to the array, which is what you want.

淡淡的优雅 2024-10-03 03:50:00

或者在不创建 LONGVALUE 类型变量的情况下尝试此操作

sendHexToASCII_UART(((LONGVALUE *)0)->bytes, 4);

or try this without creating variable of LONGVALUE type

sendHexToASCII_UART(((LONGVALUE *)0)->bytes, 4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文