Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C multi-dimensional arrays and pointers

Reply
Thread Tools

C multi-dimensional arrays and pointers

 
 
Richard Hayden
Guest
Posts: n/a
 
      11-01-2004
Hi,

Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
type' warning when compiling:

int main(int argc, char** argv) {
int testa[2][2];
int** testp = testa;
}

Whereas the following code works without warnings:

int main(int argc, char** argv) {
int testa[2];
int* testp = testa;
}

I thought that multi-dimensional arrays were implemented as arrays of
pointers to arrays of pointers?

Any insight gratefully appreciated!

Thanks,

Richard Hayden.
 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      11-01-2004
In article <cm6dud$at5$(E-Mail Removed)>,
Richard Hayden <(E-Mail Removed)> wrote:
>Hi,
>
>Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
>type' warning when compiling:
>
>int main(int argc, char** argv) {
> int testa[2][2];
> int** testp = testa;
>}


testa is an array[2] of array[2] of int:
[ [ [int1] [int2] ] [ [int1] [int2] ] ]

In most contexts (including the assignment), the array name decays to
a pointer to array[2] of int:
+-testa-decayed-to-pointer
v
[ [ [int1] [int2] ] [ [int1] [int2] ] ]


>Whereas the following code works without warnings:
>
>int main(int argc, char** argv) {
> int testa[2];
> int* testp = testa;
>}


testa is an array[2] of int:
[ [int1] [int2] ]

In most contexts (including teh assignment), the array name decays to
a pointer to int:
+-testa-decayed-to-pointer
v
[ [int1] [int2] ]


>I thought that multi-dimensional arrays were implemented as arrays of
>pointers to arrays of pointers?


Multi-dimensional arrays are implemented as arrays of arrays. Pointers
have nothing to do with it. (Don't confuse the fact that the name of an
array decays to a pointer to its first element with the implementation
thereof.)


>Any insight gratefully appreciated!


Googling for Chris Torek's posts on the subject will yield further
insight.


dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
>What problem am I missing?

Your brain is switched on, and mine wasn't.
--Gergo Barany and Chris Dollin in comp.lang.c
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-01-2004
Richard Hayden wrote:
> Hi,
>
> Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
> type' warning when compiling:
>
> int main(int argc, char** argv) {
> int testa[2][2];
> int** testp = testa;
> }
>
> Whereas the following code works without warnings:
>
> int main(int argc, char** argv) {
> int testa[2];
> int* testp = testa;
> }
>
> I thought that multi-dimensional arrays were implemented as arrays of
> pointers to arrays of pointers?
>
> Any insight gratefully appreciated!


Please see Section 6 of the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... and then come back if you're still confused.

--
(E-Mail Removed)


 
Reply With Quote
 
Robert Harris
Guest
Posts: n/a
 
      11-01-2004
Richard Hayden wrote:
> Hi,
>
> Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
> type' warning when compiling:
>
> int main(int argc, char** argv) {
> int testa[2][2];
> int** testp = testa;

int (*testp)[2] = testa;
would be OK.
> }
>
> Whereas the following code works without warnings:
>
> int main(int argc, char** argv) {
> int testa[2];
> int* testp = testa;
> }
>
> I thought that multi-dimensional arrays were implemented as arrays of
> pointers to arrays of pointers?

No (unlike Pascal). They are implemented as "flat" arrays: se section
6.5.2.1 of the standard.

Robert

>
> Any insight gratefully appreciated!
>
> Thanks,
>
> Richard Hayden.

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      11-08-2004
On Mon, 01 Nov 2004 23:02:21 GMT, Robert Harris
<(E-Mail Removed)> wrote:

> Richard Hayden wrote:

<snip>
> > I thought that multi-dimensional arrays were implemented as arrays of
> > pointers to arrays of pointers?

> No (unlike Pascal). They are implemented as "flat" arrays: se section
> 6.5.2.1 of the standard.
>

Correct about C, or nearly so; they are actually array of array, which
works out (effectively) to row-major consecutive memory. But this is
not different from Pascal, which like most HLLs has multidim arrays in
the obvious fashion -- although it tries harder than C or at least
typical C implementations to prevent out-of-bounds subscripting (or
equivalently in C only pointer arithmetic) which is the only way a
program can actually see this.

Aside from C's predecessors B and BCPL, now almost entirely vanished,
the only language I know of that requires array of pointer to array
etc. is Java, although the pointers (and elements) are "Object"s that
are implicitly dereferenced. C++ has C-equivalent arrays including
multidim, but also std::vector which if nested becomes array of
(hidden) pointer to array etc.

- David.Thompson1 at worldnet.att.net
 
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
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
casting pointers/arrays to multidimensional arrays Francesco C++ 2 11-06-2009 09:04 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments