Dear all, The linker gives following error:
*** ERROR L107: ADDRESS SPACE OVERFLOW SPACE: DATA SEGMENT: ?DT?TIMER0_ISR?TIMER LENGTH: 0002H
I have a global buffer. If i declare it idata then the linker does not give any error. But the sprintf function gives garbage values. I am unable to understand the problem.
The following is the piece of code:
dtptr = &buff; strlen = sprintf(dtptr, "%d", PrbTemperature*1000); UART_TxArray(dtptr, strlen);
PrbTemperature is a float. dtptr, buff[7] and strlen are unsigned char.
kindly assist.
char buff[] = "1234567"; char buff[7]; char buff[] = {1, 2, 3, 4, 5, 6, 7};
They all have in common that buff already is a const pointer to the buffer. You're passing a pointer to a pointer to sprintf, don't.
They all have in common that buff already is a const pointer to the buffer.
Not really. buff "is" not a pointer --- it's an array. It just automatically turns into a pointer in most of its usages.
You're passing a pointer to a pointer
No, he isn't. He's passing a pointer to an array. Because appling the address-of operator '&' is actually one of the exceptions to the above "most of" rule.
But you're still correct that &buff is not what should be passed to sprintf() --- even though it would usually work anyway. If you get a warning from your tools when you do this, you should consider that a good thing.
The Right Way(no TM) to do this is one of
sprintf(buff, ...); sprintf(&buff[0], ...); sprintf(&(buff[0]), ...);
These are all exactly equivalent, so most experienced C programmers will use the first form simply because it's shorter. But you'll find the other two forms quite frequently, too.
I do the #2 when I want to be explicit. Else I use the #1.
But I don't like type defines that are arrays - normally hide inside a struct to make the data a real black-box and avoid the array/pointer duality.