Array Assignment

Posted by Mahesh on Stack Overflow See other posts from Stack Overflow or by Mahesh
Published on 2011-03-11T22:32:17Z Indexed on 2011/03/12 0:10 UTC
Read the original article Hit count: 167

Filed under:
|

Let me explain with an example -

#include <iostream>

void foo( int a[2], int b[2] ) // I understand that, compiler doesn't bother about the
                               // array index and converts them to int *a, int *b
{
    a = b ;  // At this point, how ever assignment operation is valid.

}

int main()
{
    int a[] = { 1,2 };
    int b[] = { 3,4 };

    foo( a, b );

    a = b; // Why is this invalid here.

    return 0;
}

Is it because, array decays to a pointer when passed to a function foo(..), assignment operation is possible. And in main, is it because they are of type int[] which invalidates the assignment operation. Doesn't a,b in both the cases mean the same ? Thanks.

Edit 1:

When I do it in a function foo, it's assigning the b's starting element location to a. So, thinking in terms of it, what made the language developers not do the same in main(). Want to know the reason.

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays