Link to home
Start Free TrialLog in
Avatar of trs28
trs28

asked on

dynamic arrays - pointer problem

i'm trying to setup a dynamic array ... but its fighting with me all the way ....

in the unit header.......
     int dbfieldsSize;
     int **MappedFields;

in the constructor .....
    dbfieldsSize = sizeof(dbfields) / sizeof(dbfields[0];
    MappedFields = new int *[dbfieldsize];

I get this when building.... E2034 Cannot convert 'const int' to 'int *'


Here is the block that gives me the problem .....

for (int i = 0; i < ENDROW; i++){
            if (MappedFields[i] == sgData->Col){
                Application->MessageBox("The data column has already been mapped to the                                                              
                                                             database.","OOPS!",MB_OK|MB_ICONWARNING);
                return;
            }
        }

More specifically, it happens at    if (MappedFields[i] == sgData->Col)

(sgData is a StringGrid by the way)

I'm assuming this error message is because the "i" from the for loop isn't a pointer, but I'm just not sure how to account for this ....

Thanks!

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To elaborate - using

    int **MappedFields;

    MappedFields = new int *[dbfieldsize];

you are creating an array of arrays (actually an array of 'int*'), which is probably not what you want to do. A sregular array is created using

    int *MappedFields;

    MappedFields = new int [dbfieldsize];

Now, if you apply 'operator[]' to 'int **MappedFields;' as you did, you get an 'int*, thus the error the compiler is complaining about.

... and some more details on that very issue here: http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html ("C++ Notes: Dynamic Allocation of Arrays")
Avatar of trs28
trs28

ASKER

I did what you suggested in the first post and it seems to be working...i think
 (which I thought I initially tried that but I guess not)  

Tell me if this makes sense, when I'm debugging (i'm using BCB5 by the way), if I inspect the array, it only shows me one element (i.e. it only shows [0]) .

This has me a little confused .... I've tried two different values for dbfieldsSize:  3 & 15      ...but both only show one element when inspected  !?!?!  

why is this?  being that the application isn't 100% complete, its a little hard if its seeing everything properly w/o inspecting the arrays
Avatar of trs28

ASKER

i think i understand ... .its because MappedFields is a pointer, correct?
Avatar of trs28

ASKER

forget that last post ... i was thinking of it as a pointer to an element, not a pointer to an array (which is the direction i'm trying to take)
Yes, and you need to fill the array first in order to see more. I am not too familiar with BCB, but you should be able to instepct the contents when you use the memory address.
Avatar of trs28

ASKER

well, i saw this after I had already populated it .... so you're telling me that because MappedFields is a pointer to an array, it'll only show me the element that the pointer is currently on??
No, pointers and arrays are interchangeable in C/C++ - the name of an array is aplso a pointer to the array's 1st element. You can check that quite easily using

int* a = NULL;   // Pointer to int, initialize to nothing.
int n;           // Size needed for array
cin >> n;        // Read in the size
a = new int[n];  // Allocate n ints and save ptr in a.
for (int i=0; i<n; i++) {
    a[i] = 0;    // Initialize all elements to zero.
}
for (int j=0; j<n; j++) {
    cout << a[j] << endl;    // print contents
}
. . .  // Use a as a normal array
delete [] a;  // When done, free memory pointed to by a.
a = NULL;     // Clear a to prevent using invalid memory reference.

What BCB5 as a debugger will display is a different story, though.