|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Passing str |
Posted: Fri Aug 04, 2006 3:08 am |
|
|
Hi
I try to pass a string constant into a function only to receive jarbled memory as a result. Manually malloc'ing the memory and then passing the pointer in works, however. Can someone tell me why?
Function:
Code: |
void debug(unsigned char *msg)
{
int i = 0;
for(i = 0; msg[i] != '\0'; i++)
putc(msg[i]);
}
|
Call that works:
Code: |
unsigned char *buffer;
buffer = (unsigned char *) malloc(30);
strcpy(buffer, "This is a test.");
debug(buffer);
free(buffer);
|
Call that doesn't work:
Code: |
debug("This is a test");
|
I will add that I am using a PIC18F2525. |
|
|
Ttelmah Guest
|
|
Posted: Fri Aug 04, 2006 4:21 am |
|
|
Read the manual....
It is in the 'frequently asked questions', under 'How can a constant data table be placed in ROM'.
You cannot have a pointer to a constant string, and though you use the 'array' construct to try to access the data in the function, the actual construct is using a pointer to hand the address from one location to another.
It is possible to do, using 'typemod', but this is a bulky bodge. It is a limitation of the current CCS C, bought about by the fact that the processor architecture, has seperate RAM, and ROM data spaces.
If your function is only ever going to be used with constant strings, then this will work:
Code: |
void debug(int char) {
putc(char);
}
debug("This is a test");
|
Best Wishes |
|
|
Guest
|
|
Posted: Fri Aug 04, 2006 5:11 am |
|
|
Ttelmah wrote: | Read the manual....
It is in the 'frequently asked questions', under 'How can a constant data table be placed in ROM'.
You cannot have a pointer to a constant string, and though you use the 'array' construct to try to access the data in the function, the actual construct is using a pointer to hand the address from one location to another.
It is possible to do, using 'typemod', but this is a bulky bodge. It is a limitation of the current CCS C, bought about by the fact that the processor architecture, has seperate RAM, and ROM data spaces.
If your function is only ever going to be used with constant strings, then this will work:
Code: |
void debug(int char) {
putc(char);
}
debug("This is a test");
|
Best Wishes |
Hi, thanks, I now realise that it isn't possible, but I don't understand what your example code is trying to do..? |
|
|
Ttelmah Guest
|
|
Posted: Fri Aug 04, 2006 6:31 am |
|
|
It'll do exactly what your code was trying to do!.
It is a 'non standard extra', in CCS C, that if you call a function defined to accept an int, with a constant string as the 'value', it'll repeatedly call the function, with each character in turn of the constant string. So 'debug', will be called with 'T', then with 'h', then with 'i' etc., for the whole constant string.
Best Wishes |
|
|
Guest
|
|
Posted: Fri Aug 04, 2006 7:24 am |
|
|
wow, thanks for that tip! it will definately come in handy!
although I don't need it for now as I just discovered from the manual that printf() basically does what I want, even sending through the rs232 pins!
thanks! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|