tags:

views:

209

answers:

7

I am having a function:

int getparam(char *gotstring)

and i am passing the a string argument to it i.e., a string for eg:

char *sendstring="benjamin"

instead of teh above declaration can i use

int getparam(char gotstring[])

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?

+1  A: 

In this case they mean the same thing, and you do not need to change the remainder of your function. But be aware that in general, arrays and pointers are different things.

Artelius
+3  A: 

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.

Charles Salvia
+1  A: 

Neither is better, really. It's what you're more comfortable with. Idiomatically, it is more common to use char *arg instead of char arg[] (think strcmp, strcpy, etc).

plinth
In terms of which one is actually better, I've always felt that the `char mystring[]` syntax as a function parameter is really misleading. It has historically caused endless confusion among inexperienced C programmers who mistakenly believed that the array size information was preserved in the function parameter, and that `sizeof(mystring)` would accordingly return the size of the array, rather than just the size of the pointer. I personally think this syntax shouldn't even be allowed.
Charles Salvia
+1  A: 

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.

unwind
+1  A: 

Since arrays are sent by reference in C both

int getparam(char *gotstring)

and

int getparam(char gotstring[])

means the same. But first one is used more. So using it makes you a good C citizen.

JCasso
I tend to disagree (especially with herd mentality explanation). These constructs are identical for compiler, for human they can (and should I would say) differ semantically: [] indicates passing an array, * passing a pointer to single element.
MaR
A: 

This is called passing by reference and how the name states it means to pass just a reference of the variable (in this case a char*).

Either solutions will work fine, what will actually happen is that, when void functionName(char* string) is called, the address of the first char in memory will be saved on stack and passed as a parameter to the function.

Consequently any edit to the variable inside the function would edit also the original variable. In this case, since you need to pass an array you just have this option.

For standard types like int, float you can distinguish them:

void passByValue(int i);
void passByReference(int &i);

The difference is that in the former case value is copied into stack when calling the function, while in the latter just a reference (pointer) to the value is passed.

Jack
Please I would like to know downvoting reasons, just to understand mistakes and void repeating them. A downvote without a reason is useless, especially for me. Thanks..
Jack
I didn't downvote you so I can't undo it, but presumably the downvoter is annoyed that you are confusing the idea of passing by reference with passing a pointer by value
Charles Salvia
+1  A: 

Read this article to understand when pointers and arrays are equivalent in C and C++, and when they are not.

Eli Bendersky