Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many ways are there to pass char array to function in C?

  • 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?

like image 908
mightyWOZ Avatar asked Sep 03 '15 13:09

mightyWOZ


People also ask

Can you pass char array to function in C?

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().

How many ways we can pass array to function?

There are two possible ways to do so, one by using call by value and other by using call by reference.

How many characters can a char array hold in C?

While 65,535 is a standard-specified minimum, a really small device could specify that the standard was deliberately departed from for this reason.

Is it possible to pass full array to method?

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).


2 Answers

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.)

like image 54
zneak Avatar answered Oct 01 '22 02:10

zneak


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.

like image 38
Sourav Ghosh Avatar answered Oct 01 '22 01:10

Sourav Ghosh