?
Solved

dynamic arrays - pointer problem

Posted on 2007-07-19
10
Medium Priority
?
278 Views
Last Modified: 2010-04-01
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!

0
Comment
Question by:trs28
  • 6
  • 4
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 19524874
'MappedFields[i]' represents an 'int*', whereas 'sgData->Col' is not a pointer - that is the reason. Did you mean to allocate a vector of integers or a matrix of integers? If the 1st applies (such as it seems, it does, make that

     int dbfieldsSize;
     int *MappedFields;

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

and you should be fine.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19524967
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.

0
 
LVL 86

Expert Comment

by:jkr
ID: 19525030
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

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

Author Comment

by:trs28
ID: 19525379
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
0
 

Author Comment

by:trs28
ID: 19525397
i think i understand ... .its because MappedFields is a pointer, correct?
0
 

Author Comment

by:trs28
ID: 19525414
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)
0
 
LVL 86

Expert Comment

by:jkr
ID: 19525424
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.
0
 

Author Comment

by:trs28
ID: 19525529
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??
0
 
LVL 86

Expert Comment

by:jkr
ID: 19525571
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question