Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Two dimensional pointers and Two dimensional arrays

Reply
Thread Tools

Re: Two dimensional pointers and Two dimensional arrays

 
 
John Harrison
Guest
Posts: n/a
 
      08-18-2003

"Vivek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hello,
> I know this is a pretty old topic, and must have been discussed
> time and again. I want to the exact reason why a two d array(ex. int
> arr[20][10]) can't be accessed using a two d pointer(int **p). In the
> posts which I have gone thru, state that the two d array, arr, is an
> array of arrays. So, what arr actually points to is the first element
> of the 2 d array, this element being an array of 10 integers.
> The thing which is not clear is: Consider int b[10], and int * q.
> Here b is an array of 10 integers,and can be accessed using the
> pointer q.


Simple

int* p;
int a[10];

Each element of a is an int, p points to an int. SAME.

int** p2;
int a2[10][10];

Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.

That's the reason.

john


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-18-2003
John Harrison wrote:

>
> Simple
>
> int* p;
> int a[10];
>
> Each element of a is an int, p points to an int. SAME.
>
> int** p2;
> int a2[10][10];
>
> Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.
>
> That's the reason.
>


That was a great explanation.

Here's another way of looking at it. p2 is a "pointer to pointer to
int". Therefore, it can point to a "pointer to int". Where exactly is
this "pointer to int" to which p2 should point? There certainly is no
such thing in a2.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      08-19-2003
Vivek <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "John Harrison" <(E-Mail Removed)> wrote in message

news:<bhq2sh$1se1v$(E-Mail Removed)-berlin.de>...
> > Simple
> >
> > int* p;
> > int a[10];
> >
> > Each element of a is an int, p points to an int. SAME.
> >
> > int** p2;
> > int a2[10][10];
> >
> > Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.
> >
> > That's the reason.
> >
> > john

>
> If 'int[10]' can be equivalent to 'int *'(1st case), why can't each
> element of a2, which is again int[10] be equivalent to int *, in that
> way a2 would be the same as int **, since a2 is a pointer to int[],
> anyway.


An int * contains the address of an int. An array of int contains no
addresses. It consists of nothing but a sequence of int values. By assigning
an int * to the address of the first element of an array of int, you have
enough information to use the pointer as though it were an array. That is,
you can use the pointer to access every int in the array. But an array of
int is still not an int *.

DW



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-19-2003

"Vivek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "John Harrison" <(E-Mail Removed)> wrote in message

news:<bhq2sh$1se1v$(E-Mail Removed)-berlin.de>...
> > Simple
> >
> > int* p;
> > int a[10];
> >
> > Each element of a is an int, p points to an int. SAME.
> >
> > int** p2;
> > int a2[10][10];
> >
> > Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.
> >
> > That's the reason.
> >
> > john

>
> If 'int[10]' can be equivalent to 'int *'(1st case), why can't each
> element of a2, which is again int[10] be equivalent to int *, in that
> way a2 would be the same as int **, since a2 is a pointer to int[],
> anyway.


It could be, but it isn't because C wasn't designed that way.

What you asking for is that multidimensional arrays be stored as an array of
pointers, but they aren't.

john


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-19-2003
Vivek wrote:

> If 'int[10]' can be equivalent to 'int *'(1st case),



They are NOT equivalent. In certain circumstances (well most) the name
of an array is converted to a pointer to the first element. In other,
very important, circumstances this conversion is not done.

> why can't each
> element of a2, which is again int[10] be equivalent to int *, in that
> way a2 would be the same as int **, since a2 is a pointer to int[],
> anyway.


C++, like C, does not support two-dimensional arrays. What you really
have are arrays of arrays. As such, a pointer to pointer can't work
because the compiler doesn't know the stride, i.e. how much to move
within the assigned memory for element access.

Think about it, you'd have the same **p for any two-dimensional array,
regardless of the element size. The compiler knows how to move within
the memory by the type of element pointed to. I wish I was one of these
guys that can make cool ASCII art to illustrate, but I'm not.




Brian Rodenborn
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
C multi-dimensional arrays and pointers Richard Hayden C Programming 4 11-08-2004 09:39 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments