• C

Character Arrays In Functions

I'm having a problem when returning a character array from a function.

I've got:

char * function()
{
char myCharacter[] = "hello";

char *newPointer = newString;

return newPointer;
}

int main()
{
printf("%s", function());

return 0;
}

I get a strange answer.. I'm assuming something is wrong with my pointers and all.. could someone help.
cfansAsked:
Who is Participating?
 
dimitryCommented:
char * function()
{
char myCharacter[] = "hello";  // <<<<< Means stack allocation that will be gone when you will leave the function...

char *newPointer = newString; // You meant newPointer = myCharater; ???

return newPointer;
}

Try:
char * function()
{
static char myCharacter[] = "hello";  // <<<<< Data Segment allocation

char *newPointer = myCharacter;

return newPointer;
}

0
 
cfansAuthor Commented:
I don't quite understand why using "static" makes the difference, could you explain that for me?
0
 
PaulCaswellCommented:
Hi cfans,

dimitry is correct. A string, in C, is just a pointer to some memory. When you do:

char * function()
{
 char newString[] = "Hello";

Both the 'newString' pointer AND the "Hello" are placed on the stack (usually). When the function returns 'newString, it is actually returning a pointer to the area of the stack that had the "Hello" in it. Sadly, once 'function' exits, this memory becomes available for other functions to use ('printf' in this case) so, although at the moment you return from 'function' the string is valid and the pointer points to it, by the time printf gets to printing the string, it has been destroyed.

The easiest option, as dimitry has pointed out, is to make the string 'static'. This forces it out of the stack and into your data area and it therefore does not get destroyed. The downside of this is that EVERY time you call the function, the same address will be returned to the same memory. So if you do something like:

 char * str1 = function();
 char * str2 = function();
 strcpy(str2,"bye");
 printf("%s %s", str1, str2);

You will actually get "bye bye" printed because both str1 and str2 point to the same memory.

The common way to get around this is to use 'malloc'.

char * function ()
{
 char * newString = (char*)malloc(10);
 strcpy(newStr,"Hello");
 return newStr;
}

you could then do:

 char * str1 = function();
 char * str2 = function();
 strcpy(str2,"bye");
 printf("%s %s", str1, str2);

and you would get "hello bye" as you would expect.

There is one other method:

typedef struct ShortString
{
 char str[10];
} ShortString;

ShortString function ( void )
{
 ShortString s = {"Hello"};
 return s;
}

ShortString str1 = function();
ShortString str2 = function();

This is a little more complicated to understand but essentially, we are wrapping the string up in a structure so it can be returned by value and not as a pointer.

Paul
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.