4

i don't know where is the problem i'm assigning the address to other 2 dimensional array. Please help me to fix this problem

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

prog.cpp:8:9: error: invalid array assignment

4 Answers 4

4

You can't copy an array using =. Neither can you assign an array's address; x = y; doesn't work either when x and y have types char[1] for example. To copy the contents of b to a[2], use memcpy:

memcpy(a[2], b, sizeof(a[2]));
Sign up to request clarification or add additional context in comments.

Comments

2

you should iterate each element of b[2] one by one and store into it a[2] try this:

int main()
{
    int a[3][2];
    int b[2]={0,1};
    for(int i=0;i<2;i++){
     b[i]=a[2];
    return 0;
}

Comments

2

Because the standard says so. Arrays cannot be assigned, only initialized. so C:

int i;
for(i = 0; i < 2; i++)
{
  a[2][i] = b[i];
}
...

C++: you can use strcpy!

1 Comment

oh, you can use memcpy
0

This assignment is not possible.Array allow specific location assignment. you can try like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.