The problem is simply that you didn't allocate any space for "string" - you just declared a pointer. Here's one possible solution:
Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char * getstring (char *buffer, int buflen);
int
main()
{
char *s = malloc (10);
printf("%s\n",
getstring(s, 10));
free (s);
return 0;
}
char*
getstring(char *buffer, int buflen)
{
strncpy(buffer,"text",buflen);
return buffer;
}
You certainly don't need the "malloc()" - you could just as easily have declared "char s[10]" and accomplished the same thing (and eliminated the call to "free()" at the same time).
Similarly, strncpy() copy is arguably "safer" than "strcpy()" in preventing buffer overruns - but unfortunately, it, too, has its own problems.
Bottom line: allocate string space, and your program is fine!
'Hope that helps ..