Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning string from a function (fairly urgent =) ) 1

Status
Not open for further replies.

PetitPal

Programmer
May 2, 2001
243
GB
OK, I know I'm probably going mad BUT this project is driving me mad now, so...

- to return a string from a function you need to use a pointer?

- this code (as an example) should work...?


void change_it(char *my_string) {
*my_string = "hello\0";
return;
}

void main (void) {
char sMyString[10] = "test\0";
char *pMyString = &sMyString;

change_it(pMyString);
}


Am I right in thinking that this would change the value of sMyString to 'hello' in the main routine, via the pointer pMyString?

If not, could anyone please provide an example? Thanks and sorry for asking but I'm trying to write code for devices which have v. small stacks (<16K) and I'm not sure if it's my code that's wrong or just the fact that the pointers aren't working because of the size / style of the stack.

Thanks!

PetitPal.
 
you can NOT set strings = to constant strings as you are doing.

First you need to make sure the string you are passing has enough memory allocated for the lenght of the new string and 2, you need to use sprintf, strcpy, or various other methods depending on platform

so your function above would now look like

strcpy(my_string,&quot;Hello&quot;);



Matt
 
*doh* - I knew I was going mad (too my languages and too long staring at the screen!); anyway - it works now so thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you, thank-you (etc)

=)

PetitPal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top