foo(char *s)
foo(char *s[ ])
foo(char s[ ])
What is the difference in all these ?
Is there any way in which I will be able to modify the elements of the array which is passed as argument, just as we pass int
or float
using &
and value of actual arguments gets modified?
How Arrays are Passed to Functions in C/C++? A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().
There are two possible ways to do so, one by using call by value and other by using call by reference.
While 65,535 is a standard-specified minimum, a really small device could specify that the standard was deliberately departed from for this reason.
You can pass an entire array, or a single element from an array, to a method. Notice that the int [ ] indicates an array parameter. Notice that passing a single array element is similar to passing any single value. Only the data stored in this single element is passed (not the entire array).
It is not possible in C to pass an array by value. Each working solution that you listed (which unfortunately excludes #2) will not only let you, but force you to modify the original array.
Because of argument decay, foo(char* s)
and foo(char s[])
are exactly equivalent to one another. In both cases, you pass the array with its name:
char array[4];
foo(array); // regardless of whether foo accepts a char* or a char[]
The array, in both cases, is converted into a pointer to its first element.
The pointer-to-array solution is less common. It needs to be prototyped this way (notice the parentheses around *s
):
void foo(char (*s)[]);
Without the parentheses, you're asking for an array of char pointers.
In this case, to invoke the function, you need to pass the address of the array:
foo(&array);
You also need to dereference the pointer from foo
each time you want to access an array element:
void foo(char (*s)[])
{
char c = (*s)[3];
}
Just like that, it's not especially convenient. However, it is the only form that allows you to specify an array length, which you may find useful. It's one of my personal favourites.
void foo(char (*s)[4]);
The compiler will then warn you if the array you try to pass does not have exactly 4 characters. Additionally, sizeof
still works as expected. (The obvious downside is that the array must have the exact number of elements.)
Scenario: You're calling the function with an array as argument.
In that case,
foo(char *s)
and
foo(char s[])
are considered equivalent. They both expect to be called with a char
array.
OTOH, foo(char *s[ ]),
is different, as it takes the address of a char *
array.
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