Saturday, 14 September 2013

Is it possible to assign a new value to an array name?

Is it possible to assign a new value to an array name?

I read that;
Although an array name can be used as a pointer (after decaying into
pointer), it's not possible to assign it a new value. Attempting to make
it point elsewhere is an error:
while (*a != 0) // a is of (int *) type
a++; //wrong
On the other hand, when passed to a function, an array name is always
treated as a pointer. The function call
largest = largest_num(b, n) // b is an array of int
for the function
int find_largest(int a[], int n)
{
....
....
}
causes a pointer to the first element of b to be assigned to a.
Above two statements ( in bold ) seems to me contradictory. I am confused.

No comments:

Post a Comment