I am having a function:
int getparam(char *gotstring)
and i am passing a string argument to it, like char *sendstring = "benjamin"
Instead of the above declaration I can use,
int getparam(char gotstring[])
Question: Which one is better? And if I have to use int getparam(char gotstring[])
what are all the other changes I have to make to the existing function?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With