473,394 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Double pointer and 2D array


I have a double pointer and a 2D array:

int mat[5][5], **ptr;
ptr = mat;

mat[2][3] = 3;

Although mat and ptr are pointing at the same address,
&ptr[2][3] and &mat[2][3] are not, why?

I know ptr[2][3] dereferences the resulting object two
full time which will have the address of whatever content
that is at [2][3] rather than the address TO [2][3] but I
can't figure out why?

Thank you.
Nov 14 '05 #1
5 23081
ur**@ur8x.com wrote:
I have a double pointer and a 2D array:

int mat[5][5], **ptr;
ptr = mat;

mat[2][3] = 3;

Although mat and ptr are pointing at the same address,
&ptr[2][3] and &mat[2][3] are not, why?

I know ptr[2][3] dereferences the resulting object two
full time which will have the address of whatever content
that is at [2][3] rather than the address TO [2][3] but I
can't figure out why?

Thank you.


Hi,

You need to declare ptr as "a pointer to an array of 5 integers":

int (*ptr)[5];

You should have gotten a warning from the compiler about your
ptr = mat;
statement. Warnings are our friend!

The compiler needs to know, for a multi-demensional array, how
to index into it. The 5 in the pointer declaration above is the
size of the last array in mot.

If mot had 3 dimensions, e.g.:

int mot[3][4][5];

Then ptr would have to look like:

int (*ptr)[4][5];

It turns out that the compiler doesn't need the first dimension
to index into an array.

-Rich

--
Richard Pennington
Email: ri**@pennware.com
http://www.pennware.com ftp://ftp.pennware.com

Nov 14 '05 #2
ur**@ur8x.com wrote:
I have a double pointer and a 2D array:

int mat[5][5], **ptr;
ptr = mat;

mat[2][3] = 3;

Although mat and ptr are pointing at the same address,
&ptr[2][3] and &mat[2][3] are not, why?

I know ptr[2][3] dereferences the resulting object two
full time which will have the address of whatever content
that is at [2][3] rather than the address TO [2][3] but I
can't figure out why?

Thank you.


First, 'int mat[5][5]' is simply 25 ints. A pointer to it would be
'int (*ptr)[5] = mat;'. Now ptr[0][0] is the first of them and
ptr[4][4] is the last of them.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #3
Joe Wright <jo********@comcast.net> wrote:
First, 'int mat[5][5]' is simply 25 ints. A pointer to it would be
'int (*ptr)[5] = mat;'. Now ptr[0][0] is the first of them and
ptr[4][4] is the last of them.


Ok, my question is, assuming my first post, that is ptr = mat, why
ptr[0][0] holds an address (I know it does not get the intended
answer, I want to know why).

P.S. Is there a difference between `int * ptr[5]' and `int (*ptr)[5]'?

Nov 14 '05 #4
ur**@ur8x.com wrote:
Joe Wright <jo********@comcast.net> wrote:
First, 'int mat[5][5]' is simply 25 ints. A pointer to it would be
'int (*ptr)[5] = mat;'. Now ptr[0][0] is the first of them and
ptr[4][4] is the last of them.

Ok, my question is, assuming my first post, that is ptr = mat, why
ptr[0][0] holds an address (I know it does not get the intended
answer, I want to know why).

P.S. Is there a difference between `int * ptr[5]' and `int (*ptr)[5]'?


int **ptr;

"ptr is a pointer to a pointer to int."

In the following, the offsets are in bytes (no pointer arithmetic)
and sizeof(int) is assumed to be 4.

ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4))

int (*ptr)[5];

"ptr is a pointer to an array of 5 int."

ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4 * 5))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4 * 5))

Where the 5 above is the number of array elements in the second
dimension.

Quite different addresses!

int *ptr[5];

"ptr is an array of 5 pointers to int."

Note that the precedence of [] is higher than *.

-Rich

--
Richard Pennington
Email: ri**@pennware.com
http://www.pennware.com ftp://ftp.pennware.com

Nov 14 '05 #5
Richard Pennington <ri**@pennware.com> wrote:
int **ptr; "ptr is a pointer to a pointer to int." In the following, the offsets are in bytes (no pointer arithmetic)
and sizeof(int) is assumed to be 4. ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4)) int (*ptr)[5]; "ptr is a pointer to an array of 5 int." ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4 * 5))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4 * 5)) Where the 5 above is the number of array elements in the second
dimension. Quite different addresses! int *ptr[5]; "ptr is an array of 5 pointers to int." Note that the precedence of [] is higher than *.


Excellent, now it makes sense. A double pointer just simply
does not skip the width (or the column) of the array since
it's, well a double pointer, not a pointer to the array of
such dimension.

Thank you for your help.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
10
by: Jacek Dziedzic | last post by:
Hi! Let's say I have a class called Triplet that serves as an envelope for double, ie. class Triplet { public: Triplet() {/*...*/} /* some things that double doesn't have, like
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
0
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib...
0
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
5
by: Kiran | last post by:
Hi all, another newbie question from me, but here goes. Ok, I have a double array defined as follows: int cluster; now, i know that if you say for example cluster, this will give me the...
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
8
by: Muzammil | last post by:
Hi.Can any one help me using double pointer with template on any class. i m confuse in using double pointer.its my class . //dynamic 2-d array class template <class t> class dsarray {...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.