0

I am trying to initialize a multidimensional array in batches and can't seem to make it work or find an example!

The dimensions I am working with are big enough that I don't want to specify them by hand!

More precisely :

int test[5][192];
for(int i = 0; i < 5; i++){
 int temp[192] = {...};
 test[i] = temp;
}
// use variable test here..

I want to use this method because the temp array is dynamicaly defined depending on variable i.

Is this type of initialization possible?

Should the temp array be in dynamic memory?

Since after the initialization I pass a reference to the first element of test to another function and I am not in control of how the other function passes over the elements I need to keep the data type of an array!

asked Jun 23, 2015 at 14:09
2
  • You'll get an invalid array assignment if you do it like that i believe. Commented Jun 23, 2015 at 14:12
  • Not duplicate since the goal is to keep the simple array data-type Commented Jun 23, 2015 at 14:38

4 Answers 4

3

If you want to copy the values of temp array , instead of "=", you should use memory copy

memcpy( test[i], temp, sizeof(temp[192]));
answered Jun 23, 2015 at 14:12
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be what I was looking for! Will give it a try! Thank you
1

Arrays do not have the copy assignment operator. So this is impossible with arrays.

If you will dynamically allocate each row then in any case you have to store somewhere the number of their elements. So even dynamically allocated arrays are not suitable in this case when the numbers of elements in each row can differ.

You should use standard container std::vector<std::vector<int>> instead.

answered Jun 23, 2015 at 14:12

Comments

0

You dont use the second dimension in the array for the test array. you just write test[], but you must write test[][]. I thing you don't must use the temp array. You can initialize you array direct, without using a temp array.

I'm not absolutly sure, but memcpy are just using for one diemnsionalarrays ant not for multidimensional arrays

answered Jun 23, 2015 at 14:34

1 Comment

Since a simple array is equivalent to a multidimensional array, the code works just fine!
0

if you know, Temp and test[i] are two pointer which point to memory so if you print temp or test[i] you will see the address of where they start on memory. in your code you lose the address of test[i] because you changed pointer test[i] to temp and now both of them are pointing to the same place where temp begin on there!

answered Jun 23, 2015 at 15:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.