CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

passing a const string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
russk2txb



Joined: 30 Mar 2008
Posts: 109
Location: New Jersey

View user's profile Send private message

passing a const string
PostPosted: Fri Feb 04, 2011 8:57 am     Reply with quote

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 9:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 9:42 am     Reply with quote

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: 19371

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 9:47 am     Reply with quote

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 10:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 3:09 pm     Reply with quote

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 3:21 pm     Reply with quote

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: 19371

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 4:17 pm     Reply with 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.

Best Wishes
russk2txb



Joined: 30 Mar 2008
Posts: 109
Location: New Jersey

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 4:37 pm     Reply with quote

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 4:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 5:05 pm     Reply with quote

Quote:
PIC18F4680, Ver 4.083

#DEVICE PASS_STRINGS=IN_RAM

No info about PASS_STRINGS in my CCS manual???

Your compiler was released in late November 2008, so probably the
closest manual would be March 2009.
http://www.icabots.com/Descargas-Manuales/ccs_c_manual2009.pdf

Or August 2009 might be useful:
http://elmicro.com/files/ccs/ccs_c_manual.pdf
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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