View previous topic :: View next topic |
Author |
Message |
russk2txb
Joined: 30 Mar 2008 Posts: 109 Location: New Jersey
|
passing a const string |
Posted: Fri Feb 04, 2011 8:57 am |
|
|
This is driving me nuts:
If I write: Code: |
void SomeFunc (char *s);
SomeFunc ("test"); |
I get a compile error that cannot create a pointer to a constant string. I tried declaring s as 'const char*', but the compiler does not seem to understand that.
Yet I know it can be done because it works with printf. But try as I can I cannot find the declaration for printf so I can see how it is done.
This should be simple but I cannot figure it out - help?
Thanks, Russ |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Feb 04, 2011 9:30 am |
|
|
Which PIC are you using and what's your compiler version?
In the meantime, you have a couple options...
1: Copy your string to RAM first, then pass the RAM array to your function.
2: #DEVICE PASS_STRINGS=IN_RAM
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
russk2txb
Joined: 30 Mar 2008 Posts: 109 Location: New Jersey
|
|
Posted: Fri Feb 04, 2011 9:42 am |
|
|
Quote: | Which PIC are you using and what's your compiler version? |
PIC18F4680, Ver 4.083
Quote: | In the meantime, you have a couple options...
1: Copy your string to RAM first, then pass the RAM array to your function. |
That is what I am doing now, but it does not seem like it should be necessary.
Quote: |
2: #DEVICE PASS_STRINGS=IN_RAM |
No info about PASS_STRINGS in my CCS manual???
Thanks Ben, Russ |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Feb 04, 2011 9:47 am |
|
|
It is in the manual.
Look under #device.
You also may be able to use the ROM declaration, rather than the const declaration, that rom based constants, that support pointer construction. Your compiler is 'borderline' on this starting to work (it is documented in the readme, with the compiler, rather than in the manual).
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Feb 04, 2011 10:33 am |
|
|
russk2txb wrote: | Quote: | Which PIC are you using and what's your compiler version? |
PIC18F4680, Ver 4.083
Quote: | In the meantime, you have a couple options...
1: Copy your string to RAM first, then pass the RAM array to your function. |
That is what I am doing now, but it does not seem like it should be necessary.
|
You have to remember the Architecture of the PIC.
Because RAM and ROM are separate and the logic of the CPU generally operates on RAM, you should have the string in RAM. But then what about storing stuff permanently? Well, that goes in ROM.
Thus the problem.
The solution is to copy the string from ROM to RAM and then work on the ram...
Eventually, CCS added the feature that this is done somewhat automatically with the PASS_STRINGS=IN_RAM added to the #DEVICE pre-processor. (maybe because they were fielding so many calls with new users not understand how the PIC Architecture impacts things like constants -> RAM operations. Who knows?)
ANYway.....
Quote: |
2: #DEVICE PASS_STRINGS=IN_RAM |
No info about PASS_STRINGS in my CCS manual???
[/quote]
Because your compiler is as old as it is -- try the #DEVICE method and if it doesn't work - use the older method as your compiler isn't current and doesn't support passing strings in RAM by the #DEVICE directive.
Cheers,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
russk2txb
Joined: 30 Mar 2008 Posts: 109 Location: New Jersey
|
|
Posted: Fri Feb 04, 2011 3:09 pm |
|
|
Thanks all for the comments.
Actually I kind of understood what the problem is, but still - - - Howcome printf works? I mean you can pass a constant (ROM) string to printf and the compiler does not complain. So there must be some way that printf is declared in such a manner that a ROM string can be passed, and the complier doesn't barf, and the string is printed. I just want to do the same thing.
Maybe it isn't worth it to try to emulate that behavior.
Thanks again, Russ |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Feb 04, 2011 3:21 pm |
|
|
russk2txb wrote: | Thanks all for the comments.
Actually I kind of understood what the problem is, but still - - - Howcome printf works? I mean you can pass a constant (ROM) string to printf and the compiler does not complain. So there must be some way that printf is declared in such a manner that a ROM string can be passed, and the complier doesn't barf, and the string is printed. I just want to do the same thing.
Maybe it isn't worth it to try to emulate that behavior.
|
That's a fair question which hopefully someone more in the know could answer.
I could speculate that since printf is a char-by-char function, there is in a sense, that same "Pass strings in RAM" operation to them. Why that same ease wasn't duplicated in string functions, I have no idea. (shrug)
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Feb 04, 2011 4:17 pm |
|
|
You need to remember that CCS controls how they interpret things for functions like printf. Printf, is basically 'overloaded', and has two versions, switching as needed to automatically handle copying the const, if given a const string.
Best Wishes |
|
|
russk2txb
Joined: 30 Mar 2008 Posts: 109 Location: New Jersey
|
|
Posted: Fri Feb 04, 2011 4:37 pm |
|
|
Ttelmah writes:
Quote: | You need to remember that CCS controls how they interpret things for functions like printf. Printf, is basically 'overloaded', and has two versions, switching as needed to automatically handle copying the const, if given a const string. |
Ok, so how do you overload a function in straight C? I want to do that too!
Also, it seems like there is still the basic question: How can they get the compiler not to complain when a ROM string is passed. Even if overloaded, you are still passing a pointer to ROM, to some function.
Russ |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Feb 04, 2011 4:51 pm |
|
|
russk2txb wrote: |
Ok, so how do you overload a function in straight C? I want to do that too!
|
It's in the help file. If it's not in your help file (I searched for "overload" and got "Overloaded Functions" as one of the results), your compiler may not support it if it's old.
Quote: |
Also, it seems like there is still the basic question: How can they get the compiler not to complain when a ROM string is passed. Even if overloaded, you are still passing a pointer to ROM, to some function.
|
for now, if you want, pass strings around by copying the string to RAM first.
So you might have:
Code: | char const hello_string [] = "hello";
void my_function ( void) {
char string[20];
strcpy(string, hello_string);
printf("String is now: %s\r\n", string);
} |
_________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|