Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C pointer syntax

Name: Anonymous 2007-11-14 12:42

If I declare an array of arrays like this:

const char** p0 = { "##  ", "##  ", "    ", "    " };

I get these warnings:
type_d.h:13: warning: initialization from incompatible pointer type
type_d.h:13: warning: excess elements in scalar initializer
type_d.h:13: warning: (near initialization for ‘p0’)
type_d.h:13: warning: excess elements in scalar initializer
type_d.h:13: warning: (near initialization for ‘p0’)
type_d.h:13: warning: excess elements in scalar initializer
type_d.h:13: warning: (near initialization for ‘p0’)

but if I alter the syntax to

const char* p0[] = { "##  ", "##  ", "    ", "    " };

the warnings are all gone. what's exactly the difference between using * [] and ** ? I really thought they were just the same, just like * and [] or ** and [][].

If I did it like p0[][] = { ... would it be different at all?

Name: Anonymous 2007-11-18 11:56

>>1
Just read the declarations. When you declare "const char** p0" you are saying "pointer to a pointer to a const char". Declaring "const char* p0[]" you are saying "array of pointers to const chars".

From my POV: C pointers are NOT related to arrays in any way. The confusion only arises from the (very specific) fact that pointers can be "indexed" like arrays. Other than this, they have nothing to do with each other. Have this in mind when using sizeof -- notice how it behaves differently when used with pointers and arrays.

Exception made for [] arrays as formal parameters since those will act like pointers as far as I know. Also, cocks.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List