Coming from a PHP background, I'm used to writing small functions that return a string (or the response from another function) like so:
function get_something(){
return "foo";
}
However, I'm new to C and am trying to figure how to do some really fundamental things like this.
Can people review the following similar functions and tell me how they differ and which one is the best/cleanest to use?
char *get_foo(){
char *bar;
bar = "bar";
return bar;
}
char *get_foo(){
char *bar = "bar";
return bar;
}
char *get_foo(){
char *bar = NULL;
bar = "bar";
return bar;
}
char *get_foo(){
return "bar";
}
Is there any difference between these functions or is this a style issue?
One other thing. If I have two functions and one calls the other, is this alright to do?
char *get_foo(){
return "bar";
}
char *get_taz(){
return get_foo();
}
UPDATE: How would these functions need to change if get_foo() did not return a const char*? What if get_foo() calls another function that has a char* of different lengths?