Originally Posted by
Abda92
But just to make things clear, you can not return a string from a function in any way?
Thanks
Not a local string (i.e. an array of chars). You can do either of these:
Code:
char* func()
{
char* str = malloc( 80 * sizeof( char ) + 1 );
strncpy( str, "Hello World", 80 );
return str; /* Now the calling function must free() the string. */
}
or
Code:
void func( char* str, unsigned int size )
{
strncpy( str, "Hello World", (size - 1) );
}
The second method is usually better since the person calling the function knows for sure that they are responsible for memory allocation, plus then they also know the amount of memory allocated.