Hi, I want to know whether below code will perform expected task or not ?
void test(unsigned char const xdata* buffer);
unsigned char magicString[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void main(void) { unsigned char i;
while(1) { for(i=0; i<8; i++) { magicString[i] += 1; } test(magicString); } }
void test(unsigned char const xdata* buffer) { unsigned char j;
for (j=8; j>0; j++) { send(*buffer++); // another function } }
Will test() and send() function works ok ? Will send() will send all 8 byte of data of magicString or not ? is there any logic mistake ?
Thanks in advance ?
Well, my processor if basically a dual core consist of C51 MPU code and DSP Compute Engine core. But i think the problem is independent of processor, and somehow related to the code generated by Keil. There are 2 WD Timers. One is hardware and one is 8051 s/w. I disabled both, so no issue with rebooting by WD timer reset.
The strange behavior i observed in above code snippet is, when i call the test() function as below, system is misbehaving:
void main(void) { unsigned char i; while(1) { for(i=0; i<8; i++) { magicString[i] += 1; } test(&magicString); // Passing address } } void test(unsigned char const xdata* buffer) {...}
But if i called the test() as below in above code, the system works fine:
test((unsigned char xdata *)&magicString); //Casting test(magicString); // W/o casting
Its hard time for me to understand whats wrong with passing address of magicString i.e. test(&magicString) ?
Can anybody explain me why the last 2 test() work fine and not the first.
"&magicString" is not the address of the first element of the array magicString[]. That's where the error is.
What you actually want is "&magicString[0]" or just "magicString", which both are a pointer to the first element of magicString.
""&magicString" is not the address of the first element of the array magicString[]."
If it isn't, then what is it?
The types that they point to are different but the address should be same. That's my understanding anyhow. (I haven't got a copy of the C standard before you ask though!)
void test(unsigned char const xdata* buffer) { unsigned char j; for (j=8; j>0; j--) { send(*buffer++); // another function } }
Should you be able to increment a const pointer passed as a parameter to a function?
"buffer" is not const, given that declaration. It points to a const char, but is not itself const.
const is left-associative. (If there's nothing to its left, it will modify the thing to its right.)
Another way to look at it is that anything to the left of the * describes the type pointed to; anything to the right describes the pointer itself.
The types that they point to are different but the address should be same.
Yes, that's right. I had hoped the person who thought otherwise would realise this in the process of trying to answer the question.