Solved

Array of Pointers - Indexing the array as a multidimensional array - Question

Posted on 2006-06-12
4
282 Views
Last Modified: 2012-05-05
AStruct* P[500];

cout << P[5][10]->astring <<endl;

Please tell me if I am correct.

This does not work because the compilers sees that P is an array of pointers.
So, the compiler processes the first bracker [5] and determines which pointer to 'use'
When it encounters the [10], it assumes that it is now dealing with the 10th memory offset of pointer AStruct, which should lead to some bad memory access issues.

I'm just trying to confirm some of my suspicions.

I'm also curious if it is possible to access an array of pointers via brackets as a multidimensional array.

It's a bit annoying to have to do a P[5 * RowLength + 10]
0
Comment
Question by:oxygen_728
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 500 total points
ID: 16883934
>> cout << P[5][10]->astring <<endl;

It should be like this
cout << P[5][10].astring << endl;
0
 

Author Comment

by:oxygen_728
ID: 16883971
I had some implementation issues, i thought you could do what I was doing above.

Thanks for the tips in this post and the previous
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16883974
>> I'm also curious if it is possible to access an array of pointers via brackets as a multidimensional array.
Possible.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16883981
>> Thanks for the tips in this post and the previous
my pleasure :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

830 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