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

string in parameter

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



Joined: 16 May 2008
Posts: 14

View user's profile Send private message

string in parameter
PostPosted: Fri Jun 27, 2008 7:06 am     Reply with quote

Hello,

in display_word() definition what must be in parameter instead of ???

////////

void display_word( ??? ) {}

void main (void)
{
display("hello")
}

thanks, julien
Ttelmah
Guest







PostPosted: Fri Jun 27, 2008 9:01 am     Reply with quote

This is more complex than it sounds!...
In C, all 'string' implies, is a single dimensional array of characters, terminated with a '\0'.
Some other languages have 'strings' as a full type.
Now, if you have such an array, and want to pass it to a function, then normally, all you would do, is pass the _address_ of the array (so you don't end up moving large amounts of data unnecessarily. So, since in C, the 'name' of an array, is 'shorthand' for the address, you would use:
Code:

char array[20];

function(array);

//then the array would have it's definition as:

void function(char array[]) {

//or

void function(char * array) {

In 'C', array definitions, and pointers are interchangeable.

Now, the problem here is what you show in your original example. You show a _constant_ string. Now on the older PICs, it was basically impossible to access the program memory directly, so you can't construct 'pointers', to a constant array. So, what you show, can't directly be done!...
Two routes round.
First, if you make a copy of the data, into RAM, this can then be used. Strcpy, allows this to be done. So:
Code:

char array[20];

strcpy(array,"hello")

function(array);

On newer compilers, and chips, you can do this automatically. 'PASS_STRINGS_IN_RAM', tells the compiler to do this. You can also use the 'ROM' directive, to take advantage of the ability of the newer flash chips to directly read their ROM areas, and the compiler wll then generate a pointer to the constant array.
Secondly, on the older chips and compilers, there was a 'work around' present. If you declared the code as:
Code:

void function(int8 character) {

}

//Then called this with:

function("hello");

The compiler calls the function (which expects a _single_ character), once for each character in the constant string.
This is efficient, but means you can't then perform 'string' operations on the data.

The best route, depends what you actually want to do, and the compiler version you have.

Best Wishes
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