1

I have a 2-d array, we call arr2[3][3]. If I want to store the first row of arr2 to a new array, we call arr1[3], how would I do that?

I tried int arr1[] = arr2[0]. But it doesn't work.

1
  • "It doesn't work" isn't a problem statement. Tell us specifically the behavior you're getting. Commented Nov 6, 2013 at 3:36

2 Answers 2

2

In C you would have to copy the row of array2 into the array1 using a for loop like so..

for(i=0;i<3;i++)
    arr1[i] =arr2[0][i];
Sign up to request clarification or add additional context in comments.

Comments

1

You can either assign each element using a loop

for(int i =0; i < 3; i++)
    arr1[i] = arr2[0][i];

Alternatively, you can use memcpy.

memcpy(&arr1[0], &arr2[0][0], sizeof(int) * columnSize);

Make sure that the array being copied to has enough space when doing this approach, otherwise funky stuff will happen (easier to overlook here than with iteration)

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.