views:

44

answers:

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); 
+4  A: 

Why not just:

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

or even better:

sendHexToASCII_UART((char *)countAddr.bytes, sizeof(countAddr.bytes));
Oli Charlesworth
Then I get: `warning: pointer targets in passing argument 1 of 'sendHexToASCII_UART' differ in signedness`
Jordan S
Updating answer now...
Oli Charlesworth
I changed to (unsigned char *) and I still get the same `differ in sidnedness` warning
Jordan S
@Jordan S: that's because I'm an idiot. See newly-updated answer.
Oli Charlesworth
eureka! That did it!
Jordan S
+1  A: 

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

or:

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

Alex Farber
A: 

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.

Michel Schinz
Then I get: `warning: pointer targets in passing argument 1 of 'sendHexToASCII_UART' differ in signedness`
Jordan S
Right. Your best bet is probably to use a cast here, as suggested by Alex, since you really want to change the interpretation of your byte sequence.
Michel Schinz
A: 

or try this without creating variable of LONGVALUE type

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