Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an array argument passed to a function not a constant pointer?

Consider the code:

void foo(char a[]){
   a++;            //  works fine, gets compiled
   //... 
}

Now, consider this:

void foo(){
   char a[50];
   a++;            //  Compiler error
   //... 
}

I heard an array is equivalent to a constant pointer and can't be incremented as it is not a lvalue...

Then why does first code gets compiled, is it so because array arguments to functions are passed as a pointer, i.e. T[] is converted to T* for passing.. So, foo(a) passes a as a pointer.

But is it not back converted to T[] again because is declared as:

void foo(char a[]);
like image 877
bhuwansahni Avatar asked Apr 01 '12 08:04

bhuwansahni


People also ask

Is array a non constant pointer?

The name of the array A is a constant pointer to the first element of the array. So A can be considered a const int*. Since A is a constant pointer, A = NULL would be an illegal statement. Other elements in the array can be accessed using their pointer representation as follows.

Is an array a constant pointer?

An array name contains the address of first element of the array which acts like constant pointer. It means, the address stored in array name can't be changed.

Are arrays passed by pointer?

Since an array is passed as a pointer, the array's memory is not copied. The function uses the memory of the same array that is passed to it, and can change what is in that memory. Because arrays are already pointers, there is usually no reason to pass an array explicitly by reference.

Can array be passed as argument?

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


4 Answers

When you pass an array as an argument to a function, it decays to a pointer.
So the thing you increment inside the function body is a pointer, not an array.

like image 188
Alok Save Avatar answered Oct 10 '22 22:10

Alok Save


This is a rather unfortunate feature inherited from the C language, with the rather yucky name of "decay." Since C once did not allow passing compound types by value, they decided to allow programmers to specify arrays as function parameter types, but only cosmetically. The array type decays to a pointer type, implementing a sort of pass-by-reference semantic different from the rest of the language. Ugly.

To recap (and others have already said this), the signature

void foo(char a[]); // asking for trouble

is unceremoniously mangled into

void foo(char *a);

… all for the sake of compatibility with ancient C code. Since you aren't writing ancient C code, you should not make use of this "feature."

However, you can cleanly pass a reference to an array. C++ requires that the size of the array be known:

void foo( char (&a)[ 50 ] );

Now a cannot be modified within the function (edit: its contents can, of course — you know what I mean), and only arrays of the right size may be passed. For everything else, pass a pointer or a higher-level type.

like image 23
Potatoswatter Avatar answered Oct 10 '22 22:10

Potatoswatter


I heard an array is equivalent to a constant pointer

You can think of it that way, but they're not equivalent.

An array decays to a pointer when passed to a function, that's why inside the function it's valid.

Just because the signature is void foo(char a[]) doesn't make a an array.

Outside the function, it's just an array, and you can't do pointer arithmetics on it.

like image 26
Luchian Grigore Avatar answered Oct 10 '22 21:10

Luchian Grigore


In C++, any function parameter of type "array of T" is adjusted to be "pointer to T". Try this code:

void foo(char a[]) {}  
void foo(char* a) {}  //error: redefinition  

They are indeed the same function.

So, why can we pass an array argument to a function as a pointer parameter? Not because an array is equivalent to a constant pointer, but because an array type can be implicitly converted to an rvalue of pointer type. Also note the result of the conversion is an rvalue, that's why you can't apply the operator ++ to an array, you can only apply this operator to an lvalue.

like image 32
Cosyn Avatar answered Oct 10 '22 21:10

Cosyn