4

I'm going crazy with pointers in C at the moment. I have the following two multi-dimensional arrays:

int num0[5][3] =
{ {0,1,0},
  {1,0,1},
  {0,1,0},
  {1,0,1},
  {0,1,0}
};

int num1[5][3] =
{ {1,1,1},
  {1,0,1},
  {0,1,1},
  {0,1,0},
  {1,0,0}
};

These are then packed into another array as such:

int (*numbers[])[3] = { num0, num1 };

If I then do:

printf( "Result: %d\n", numbers[0][2][2] );

I get the expected result, in this case Result: 1.

However, I'd like to assign numbers[0] to another variable. So in a modern programming language, you'd do something as simple as:

int newvar[5][3] = numbers[0];
printf( "Result: %d\n", newvar[2][2] );

Even though my pointer knowledge is limited, I know this isn't going to work (and it of course doesn't). But for the life of me I can't figure out the correct syntax to make it work (and more importantly, understand WHY it works).

If anyone out there can help me out here I'd really appreciate it!

Thanks

2
  • 2
    With statically allocated arrays, it's not going to be easy. Really, use malloc() and a linked list-style design. That's how it's gonna be easy. Commented Jun 13, 2012 at 19:59
  • 1
    By the way, the expected results of numbers[0][2][2] is 0 not 1. Commented Jun 13, 2012 at 20:16

1 Answer 1

2

You cannot assign arrays in C, use memcpy to copy arrays:

 memcpy(newvar, numbers[0], sizeof newvar);
Sign up to request clarification or add additional context in comments.

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.