By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

C multi-dimensional arrays and pointers

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--
Er*********@sun.com
Nov 14 '05 #2

P: n/a
In article <cm**********@harrier.doc.ic.ac.uk>,
Richard Hayden <ra***@doc.ic.ac.uk> 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 dj******@csclub.uwaterloo.caWhat problem am I missing?

Your brain is switched on, and mine wasn't.
--Gergo Barany and Chris Dollin in comp.lang.c
Nov 14 '05 #3

P: n/a
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.

Nov 14 '05 #4

P: n/a
On Mon, 01 Nov 2004 23:02:21 GMT, Robert Harris
<ro*****************@blueyonder.co.uk> 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
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.