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);
Why not just:
sendHexToASCII_UART((char *)countAddr.bytes, 4);
or even better:
sendHexToASCII_UART((char *)countAddr.bytes, sizeof(countAddr.bytes));
sendHexToASCII_UART((int *)(&countAddr.bytes[0]), 4);
or:
sendHexToASCII_UART((int *)(countAddr.bytes), 4);
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.
or try this without creating variable of LONGVALUE type
sendHexToASCII_UART(((LONGVALUE *)0)->bytes, 4);
精彩评论