C multi-dimensional arrays and pointers

R

Richard Hayden

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.
 
D

Dave Vandervies

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 (e-mail address removed)
What problem am I missing?
Your brain is switched on, and mine wasn't.
--Gergo Barany and Chris Dollin in comp.lang.c
 
E

Eric Sosman

Richard said:
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.
 
R

Robert Harris

Richard said:
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
 
D

Dave Thompson

Richard Hayden wrote:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top