Solved

Need an In-depth pointer explanation

Posted on 2002-07-26
4
188 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
Comment Utility
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
Comment Utility
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
Comment Utility
Wicked.  Thanks alot for the explanation.  Pointer notation can get so crazy.  
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now