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

using string as a function parameter

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



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

using string as a function parameter
PostPosted: Sun Dec 05, 2010 9:49 am     Reply with quote

Hi guys can some one tell me how to use string as a input parameter to a function?

We can use single character as a input parameter but not a string of few characters.
Thank you.

Example:
Code:

void test (char string[5])
{
if string is "abcd" then do some thing
}
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Sun Dec 05, 2010 10:02 am     Reply with quote

You can't pass a string to a function, but you can pass a pointer.

You can do something like:
Code:

char StrBuffer[20];   // allocate space for char array

strcpy(StrBuffer,"Hello World");  // copy a string into the array
test(StrBuffer);   // pass the pointer to the array to the function

and define test() as:
Code:

void test(char *String)
{
.
.
.
}

Lookup strcpy() and strcmp() in the ccs manual. That should help.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Dec 05, 2010 10:29 am     Reply with quote

pmuldoon wrote:
You can't pass a string to a function, but you can pass a pointer.

You can do something like:
Code:

char StrBuffer[20];   // allocate space for char array

strcpy(StrBuffer,"Hello World");  // copy a string into the array
test(StrBuffer);   // pass the pointer to the array to the function

and define test() as:
Code:

void test(char *String)
{
.
.
.
}

Lookup strcpy() and strcmp() in the ccs manual. That should help.


well how do they pass string ("hello world") to the strcpy function then?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Dec 05, 2010 10:57 am     Reply with quote

Start with a distinction. "Hello World", is technically a 'constant string'. A 'string' in C, is a null terminated array of characters. You can pass a function the pointer to a string array, as already outlined. In 'normal' C, a string constant, _is_ an array of characters, and you can pass the address of this, by just using it as a value in a function that accepts such an array. In CCS, there is a problem though, as a result of the program memory, and the data memory being separate, and on some chips not directly accessible. So CCS has a 'shortcut', that on such chips, if you take a function that accepts a single character as it's parameter, and call this with a constant string, the function is repeatedly called once, for each character in the string. On the older chips, CCS, effectively 'overloads' the strcpy function, and if it receives a RAM string, behaves normally, but if it receives a constant string, it accepts it character at a time, and appends these to the target, to simulate the normal behaviour...

On later chips that can address the ROM, the compiler accepts a special parameter 'PASS_STRINGS=IN_RAM'. If this is included in a device statement at the head of the program, the compiler will automatically detect the constant string, convert it to a RAM string for you, and pass the pointer as if this string was present in the RAM memory.

So provided your chip/compiler is modern, you can use:

Code:

//right near the top of your source
#device PASS_STRINGS=IN_RAM



void test (char *string) {
   if (!strcmp(string,"abcd")) {
      //do what you want;
   }
}


test("bcde"); //This will execute the 'do what you want' code

test("abcd"); //This won't


Best Wishes
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Dec 05, 2010 7:29 pm     Reply with quote

hi Ttelmah
my compiler version is 4.084 and PIC is 16F877A
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