0

How do I do the following:

int A[2][2];
int B[2];
A[1][0]=2;
B=A[1];
printf("%d",B[0]); //prints out 2

must I use malloc? I know that in higher level languages the above method works, but I'm a little confused about what to do in C

4
  • A[1] is an array with 2 elements; B is an object ot type int. You cannot assign an array to a single variable: you can assign an array element (of the same type), with B = A[1][0]; Commented Oct 3, 2011 at 22:27
  • @pmg oops forgot to indicate that B is also an array, a 1D array Commented Oct 3, 2011 at 22:27
  • 1
    What does your "prints out 2" comment mean? The code you posted is not compilable. How can it "print out" anything? Commented Oct 3, 2011 at 22:48
  • @AndreyT note that this is not a functional code, more of a pseudo-code. B=A[1] sets array B equal to the 1d array A[1]. A[1][0]=2 so B[0]=2 Commented Oct 3, 2011 at 22:51

4 Answers 4

1

You cannot assign to arrays (though you can initialize them, which is very different).

You can do what you want, by assigning to B element-by-element

B[0] = A[1][0];
B[1] = A[1][1];

or, with a loop

for (k=0; k<2; k++) B[k] = A[1][k];
Sign up to request clarification or add additional context in comments.

2 Comments

are there more efficient ways to do that though? looping element by element seems a bit inefficient.
The compiler should optimize it for you ... but you can try a memmove or memcpy: memmove(B, A, sizeof B);. Remember to #include <string.h>.
1

The problem here is that the variable is declared as int B, when you should declare it as int B[2].

In your code, when you assign A[1] to B, it assigns the value of the first element of A[1] to B

You can declare as int *B, so you will assign the ptr of A[1] to B. As it is, depending on the compiler will not compile. To test, do:

int *B;
B = A[1];
printf("%d, %d", B[0], B[1]);

2 Comments

i made the changes a few moments ago, typo, but the question remains the same as before
Above, you will have a reference to A, thus changing B, A is also changed.
1

You could you memcpy().

#include <stdio.h>
#include <string.h>

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

  // Syntax: memcpy(to, from, number_of_bytes_to_copy);
  // (to and from should be pointers)
  memcpy(b, a[1], sizeof(int) * 2);

  printf("%d %d", b[0], b[1]);
}

Output:

3 4

Comments

0

Arrays are not assignable. You cannot assign the entire array. Whether it's 2D, 1D or 42D makes no difference here at all.

Use memcpy

static_assert(sizeof B == sizeof A[1]);
memcpy(B, A[1], sizeof B);

write your own array-copying function or use a macro

#define ARR_COPY(d, s, n) do{\
    size_t i_, n_; for (i_ = 0, n_ = (n); i_ < n_; ++i_) (d)[i_] = (s)[i_];\
  }while(0)

ARR_COPY(B, A[1], 2);

1 Comment

In other words: "You no assign array to array." :)

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.