Passing string as an argument in C
int getparam(char gotstring[])
and int getparam(char* gotstring)
are identical. Personally, I would recommend the latter syntax, because it better describes what is actually going on. The getparam
function only has a pointer to the string; it has no knowledge about the actual array size. However, that is just my opinion; either will work.
The best way to accept a string argument is
int getparam(const char *gotstring);
You can then call this using a literal string:
int main(void){ int x = getparam("is this a parameter string?"); return 0;}
Or a character array:
int main(void){ char arg[] = "this might be a parameter string"; int x = getparam(arg); return 0;}
The use of const
on the argument pointer indicates to the caller that the argument is read-only inside the function, which is very nice information.