Solved

Need an In-depth pointer explanation

Posted on 2002-07-26
4
190 Views
Last Modified: 2010-04-01
I have an array declared like this...

Float64 (* rawData[2]) [2];

It gets accessed like this...

Float64 packedData = rawData[0][0][0];
 

How is this accessed as a three dimensional array?  I am confused because, as I see it, rawData is an array of pointers to arrays of Float64's.  I just can't seem to get my head around this.  Why can't it be accessed like this...

Float64 packedData = rawData[0][0];

Can somebody give me a detailed explanation?  Perhaps a few visual aids?  I can't seem to visulize this as a 3D array.

Thanks a lot
0
Comment
Question by:kirts
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 7180346
There is a rough equivalency between a pointer to an object and an array of objects.  So if you have something like:

int *p;

you can access the CONTENTS of p with:

int q = *p;  // q gets the CONTENTS of what p points to

but also with:

int q = p[0];

In this case, p[0] is equivalent to *p.  So just extend this to greater indirection:

int **p;
int q = **p;
int q = p[0][0];

In your example declaration, you are mixing the * and [] syntax which is OK...  

So you have the equivalent of:

int (* p[2])[2];

which is declaring an array of 2 int pointers 2 times.  (The parentheses are redundant here.)

So an item in this (confusing mess) can be accessed like:

q = p[0][0][0];

or

q = *p[0][0];
0
 

Expert Comment

by:Sabnis
ID: 7180775
the thing is an array of pointers to an array
that is
Its an Array of Pointers
the rawData[2] which are pointing to a array of Float64

so u have array of pointers (that is a 2 d array) each of these pointers is pointing to an array of Float64 so there u have the 3 dimension

i cant make a diagram here but hope the above paragraph helps
0
 

Author Comment

by:kirts
ID: 7180904
Wicked.  Thanks alot for the explanation.  Pointer notation can get so crazy.  
0
 
LVL 32

Expert Comment

by:jhance
ID: 7181053
If you want to get a wizard at this, get this book:

http://www.softpro.com/0-201-60461-2.html

If you remember 10% of what's in it, you'll never miss a beat on this stuff again...
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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