2D Array problem

Hi, I'm working on my first program in C++ (switching from Java) and I'm having a problem with a 2D array and pointers.

I have 4 arrays of data structures (structs) and I need to merge/concatenate them into one large array. I'm trying to do this by filling an array with the 4 arrays I need to merge, then using for loops to combine them. Below is my code (the method "importData()" returns data-type "record*", where "record" is my struct; and "fileNames[]" is an array contained the file names of the data files I'm importing from):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int a,b,c,d,blockSize;
    record dataBlocks[4][1];
    for(int n = 0; n < 4; n++)
    {
        dataBlocks[n][0] = importData(fileNames[n]);
    }

    blockSize = sizeof(dataBlocks[0][0])/sizeof(record);
    data = new record[4*blockSize];

    for(a = 0; a < blockSize; a++)
        data[a] = dataBlocks[0][a];
    for(b = 0; b < blockSize; b++)
        data[a+b] = dataBlocks[1][b];
    for(c = 0; c < blockSize; c++)
        data[a+b+c] = dataBlocks[2][c];
    for(d = 0; d < blockSize; d++)
        data[a+b+c+d] = dataBlocks[3][d];


This is the error I'm getting:

line 5: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'record *' (or there is no acceptable conversion)

Any help would be greatly appreciated, thank you!
Last edited on
Is there any particular reason you want to do this using arrays? If not, I'd suggest vectors.
I believe you'll find them very useful in doing that sort of stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>
using namespace std;

struct Data
{
    int a;
    int b;

    Data(int a_, int b_):
        a(a_),b(b_){}
};

void init(vector<Data> & v, int size)
{
    for (int i=1; i<=size; i++)
        v.push_back(Data(i,i+size));
}

int main()
{
    //the small vectors
    vector<Data> v[4];

    //put (i+1)*2 elements
    //in vector v[i]
    for (int i=0; i<4; i++)
        init(v[i],(i+1)*2);

    //the big vector
    vector<Data> big_v;

    //concatenate the small vectors
    for (int i=0; i<4; i++)
        big_v.insert(big_v.end(),v[i].begin(),v[i].end());

    int size=big_v.size();

    //print the big vector
    for (int i=0; i<size; i++)
    {
        cout << '(' << big_v[i].a << ',';
        cout << big_v[i].b << ')' << endl;
    }

    cout << "\nhit enter to quit..." << endl;
    cin.get();
    return 0;
}

Useful links:

http://cplusplus.com/reference/stl/vector/
http://cplusplus.com/reference/stl/vector/insert/
closed account (D80DSL3A)
The second array dimension on your line 2:
record dataBlocks[4][1];
Is that a 1?
You need at least 2 elements to form an array. I'm guessing the compiler interprets the above as equivalent to:
record dataBlocks[4];
Which might make dataBlocks[n][0] a record*
Or...
The function importData() returns a record*, which is being assigned to a record. Has this approach using 4x1 arrays worked before?
fun2code wrote:
You need at least 2 elements to form an array.

Not quite :P Actually, you can even have zero length arrays ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>
using namespace std;

struct Data
{
    int size;
    char content[0];
};

void fill(Data * & pd, int size)
{
    pd=(Data*)malloc(sizeof(Data)+size);
    pd->size=size;
}

void kill(Data * pd)
{
    free(pd);
}

int main()
{
    Data * pd;

    fill(pd,4);

    pd->content[0]='H';
    pd->content[1]='i';
    pd->content[2]='!';
    pd->content[3]='\0';

    cout << "content: ";
    cout << pd->content << endl;
    cout << "size: ";
    cout << pd->size << endl;

    kill(pd);

    cout << "\nhit enter to quit..." << endl;
    cin.get();
    return 0;
}
Thanks for all your help!

As I said, I'm completely new to C++, and this is my very first program in C++. So I'm still trying to get a hang on pointers and memory references. My thought process was that I could have an array of pointers (of type 'record') that pointed to arrays (of type 'record'). So I guess I don't really need a 2D array, but I was getting the same error when I tried using a 1D array.I guess my logic is flawed somewhere.

Looks like I'll be using vectors because they are much easier to work with. But out of curiosity, why doesn't my code work?

Thanks again!
closed account (D80DSL3A)
@m4ster roshi:
Wow, I did not know that an array can have only 1 or even 0 elements! I stand corrected.
HebrewHammer wrote:
My thought process was that I could have an array of pointers (of type 'record') that pointed to arrays (of type 'record'). [...] But out of curiosity, why doesn't my code work?

Your logic is correct, you just had some trouble implementing it. If you want to use arrays, the solution should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;

struct Record
{
    int a;
    int b;
};

const int NUM=3;
const int SIZE=5;

Record * concatenate();
Record * import_data(int n);

int main()
{
    Record * big_array=concatenate();

    for (int i=0; i<NUM*SIZE; i++)
    {
        cout << '(' << big_array[i].a << ',';
        cout << big_array[i].b << ')' << endl;
    }

    //cleanup
    delete[] big_array;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

Record * concatenate()
{
    //that's what you want here
    Record * array_of_pointers[NUM];

    int i,j;

    for (i=0; i<NUM; i++)
        array_of_pointers[i]=import_data(i);

    Record * result=new Record[NUM*SIZE];

    for (i=0; i<NUM; i++)
    {
        for (j=0; j<SIZE; j++)
        {
            result[j+i*SIZE]=
                array_of_pointers[i][j];
        }
    }

    //cleanup
    for (i=0; i<NUM; i++)
        delete[] array_of_pointers[i];

    return result;
}

Record * import_data(int n)
{
    Record * data=new Record[SIZE];

    for (int i=0; i<SIZE; i++)
    {
        data[i].a=n;
        data[i].b=i;
    }

    return data;
}

The good things with vectors are that you don't have to do the cleanup manually, your 'small' containers may vary in size independently, and the number and size of your 'small' containers may be determined in run-time
(you could still pull the last two off using arrays, but it would be trickier and more verbose).

PS: Just in case you didn't notice yet, there's a very good tutorial in this site that could help a lot:

http://cplusplus.com/doc/tutorial/
http://cplusplus.com/doc/tutorial/pointers/

fun2code wrote:
Wow, I did not know that an array can have only 1 or even 0 elements!

Mmmm... After reading your post in the lounge, I thought that your first post here was a bluff to test the OP.
I guess it wasn't... Or maybe it was, and this here is a second bluff to support the first one xD
I guess you're the only one who knows for real :P
Last edited on
closed account (D80DSL3A)
My posts were sincere.
I have never thought to declare an array with fewer than 2 elements, since that seems non-sensical. However, A[0] and A[1] make perfect sense in use (just the first 2 elements of A).
Topic archived. No new replies allowed.