As a follow-up to this question:
From what I\'ve seen, this should work as expected:
void greet(){
char c[] = \"Hello\";
greetWith(c);
return;
From what I've read the safest option is to make the caller responsible for allocating memory to hold the string. You must also check if the buffer is large enough to hold your string:
char *greet(char *buf, int size) {
char *str = "Hello!"
if (strlen(str) + 1 > size) { // Remember the terminal null!
return NULL;
}
strcpy(buf, str);
return buf;
}
void do_greet() {
char buf[SIZE];
if (greet(buf, SIZE) == NULL) {
printf("Stupid C");
}
else {} // Greeted!
}
A ton of work for a simple task... but there's C for you :-) Oops! Guess I was beaten by random_hacker...