1

How do I store a list of arrays into another set of array? I tried this way but it doesn't work.

float data1[5] = {150.0, 203.0, 165.0, 4.0, 36.0};
float data2[5] = {249.0, 255.0, 253.0, 104.0, 2.0};

float allData[2] = {data1, data2};

cout << allData[1][2] << endl;     //this should print 253.0 but it has error

This didn't allow me to compile. I also tried to change it to float *allData[2] = {data1, data2}; and it allowed me to compile but I don't get the result I want.

What have I done wrong in this? Thanks.

7
  • 1
    use std::vector if developping in C++, much less pain in general :) Commented Aug 30, 2011 at 10:52
  • 1
    Using your second form, float *allData[2] = {data1, data2} actually gives you the correct answer: ideone.com/kE70q Commented Aug 30, 2011 at 10:53
  • @Sevilla: It says that "expression needs to have a pointer-to-object type". Commented Aug 30, 2011 at 12:43
  • @xEnOn: float *allData[2] is not the same as float allData[2]. Commented Aug 30, 2011 at 12:53
  • @Charles: The one with the * is a pointer while the one without the asterisk has the exact value, right? I tried float *allData[2] but got that error that says "expression needs to have a pointer-to-object type". Commented Aug 30, 2011 at 12:57

5 Answers 5

5

You should use vectors (this example is in C++11):

std::vector<float> data1 = {150.0, 203.0, 165.0, 4.0, 36.0};
std::vector<float> data2 = {249.0, 255.0, 253.0, 104.0, 2.0};

std::vector<std::vector<float>> allData = {data1, data2};
std::cout << allData[0][0] << std::endl;

Note: possibly you want to store pointers to vectors in allData to prevent copying the data, but you should always take care with such constructs as this could very quickly lead to dangling pointers. This is also the case for the solution with plain arrays by the way.

Edit, as R. Martinho Fernandes mentioned in comments:

You can change the construct of allData to:

std::vector<std::vector<float>> allData = {std::move(data1), std::move(data2)};

It's worth to note however that after this operation data1 and data2 will be emtpy as their contents are moved to allData. If you do not need them anymore this is the version to prefer (no pointers, no copying).

Sign up to request clarification or add additional context in comments.

10 Comments

Use moves instead of pointers! std::vector<std::vector<float>> allData = { std::move(data1), std::move(data2) }; is extremely cheap.
@Fernandes: The move() will copy the whole data instead of keeping the pointer?
@R. Martinho Fernandes: how stupid of me to forget about one of C++11's most important new features, thank you for reminding. Edited the answer.
@xEnOn: no, the move will probably just swap some vector internal pointers. This means you cannot expect data1 and data2 to still have the original data.
@xEnOn: yeah, Visual Studio doesn't support that initialization syntax yet. It does support the move semantics, though. Regarding what data1 and data2 would become, it is unspecified. They are however, guaranteed to still be valid objects, so you can do things like assign them something else, for example.
|
4

You can't store arrays that already exist into another array because array objects can't be moved. You can either form an array or arrays:

float allData[][5] = 
{
    {150.0, 203.0, 165.0, 4.0, 36.0},
    {249.0, 255.0, 253.0, 104.0, 2.0}
};

Or you can make your second array an array of pointers to the previous array.

float *allData[] = { data1, data2 };

or even:

float (*allData[])[5] = { &data1, &data2 };

For all of the above, the expression allData[1][2] should yield the float value 253 (the third element of the second array).

4 Comments

well, you can memcpy over the existing values from one array to another, but the array cannot overflow. So in that case, you can store one array into another but you lose the existing values.
@CodeMonkey: I'm afraid I can't understand what you mean in you comment, can you clarify?
You can use memcpy(dst,src,n) to copy one array into another. That "erases" whatever was in dst, but I was countering your statement that "You can't store arrays that already exist into another array", because you can albeit with data loss.
@CodeMonkey: That's just copying values from one array to another. What you can't do is put a named array data1 into a named array allData and expect fiddling with data1 to affect allData of vice versa.
0

You want a float[2][] for a multi-dimensional array.

Comments

0
float data1[5] = {150.0, 203.0, 165.0, 4.0, 36.0};
float data2[5] = {249.0, 255.0, 253.0, 104.0, 2.0};

float** allData = new float*[2]; 

allData[0] = &data1[0]; 
allData[1] = &data2[0];

cout << allData[1][2] << endl;     //prints 253.0

Comments

0

Using float *allData [2] is the correct way to do what you are looking for. Trying the code out using DevStudio 2010 works for me. What output are you getting and what were you expecting.

I get 253 as the output, were you expecting 253.0? In which case, you need to specify the output format for floats:

cout.precision (1); 
cout.setf (ios::fixed, ios::floatfield);
cout << allData[1][2] << endl;     // this prints 253.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.