?
Solved

Character Arrays In Functions

Posted on 2006-03-31
3
Medium Priority
?
270 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:cfans
3 Comments
 
LVL 11

Accepted Solution

by:
dimitry earned 400 total points
ID: 16346836
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
 

Author Comment

by:cfans
ID: 16347150
I don't quite understand why using "static" makes the difference, could you explain that for me?
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16347236
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question