Pointer Stew

Guy Harris guy at auspex.auspex.com
Sun May 7 20:02:05 AEST 1989


 >/* 
 >     In this program , I have defined a new variable
 >     bad_address -- which is a pointer to character. And
 >     while initializing the path array of pointers to characters
 >     I am getting the following error :
 >
 >	"program2.c", line 30: illegal initialization
 >
 >
 >	why it is so ? bad_address is also an address(pointer), which also
 >	points to character string. And as we all know that two
 >	dimensional array are implemented through pointers, i.e
 >	char a[10][20] means :
 >
 >		a is an array of 10 pointers, and each element of
 >		array a is an array of  20 characters.

Uh, speak for yourself here, not for "us all".  Many of us know no such
thing, because it's simply not true; "char a[10][20]" means "a is an
array of 10 arrays of 20 characters" - period.   Arrays are not
pointers.

The problem with the initialization in question, however, has nothing to
do with this.  "bad_address" is, indeed, a variable of type "pointer to
char", and therefore its value is of the right type to stuff into a
member of "path", since "path" is an array of pointers to "char". 
However, it is a *variable*, not a *constant*, and therefore it can't be
used in an initialization of a static variable such as "path".

>/***************/
>/* program3.c  */
>/***************/
>/* 
>     Still consider another program, where I have define 5 different
>     character arrays, and I am Initializing the *path[] with these
>     arrays, and the program works fine.

That's because "a1", "a2", etc.  are arrays of "char", unlike
"bad_address", which is a variable of type "pointer to char".  In most
(but *NOT* all) contexts, an expression of type "array of <type>" gets
converted to an expression of type "pointer to <type>" which points to
the first element of the array in question, so in those contexts "a1",
which is an expression of type "array of char", gets converted to an
expression of type "pointer to char" that points to the first element of
"a1", and the same is true for "a2", "a3", "a4", "bad_address", and
"null". 

Since "a1", etc. are static variables, the expression resulting from
that conversion is a constant expression, and thus can be used in the
initialization of a static variable such as "path".

This means that

	char bad_address[] = "Testing Initialization";

and

	char *bad_address = "Testing Initialization";

are *NOT* equivalent.  The first defines an array of "char", named
"bad_address"; that array is big enough to hold the string "Testing
Initialization", complete with the trailing '\0', and is initialized to
that string.  The second creates an array of "char", with no name, which
is big enough to hold the string "Testing Initialization", complete with
the trailing '\0', and which is initialized to that string; it defines a
pointer to "char", named "bad_address", which is initialized to a
pointer to the aforementioned unnamed array.

>/* 
>     Still consider another program, where I have define 5 different
>     character pointers, and I am Initializing the *path[] with these
>     pointers, and the program works fine.

Same problem as before; "a1", "a2", etc. are *not* constant expressions,
so they cannot be used to initialize static variables such as "path".

>.	Why character arrays can be used to initialize the array
>	of pointers to character & character pointers cannot be
>	used to initialize the array of pointers to characters ?

Because a character array of static storage duration, when converted to
a pointer in those contexts where such a conversion is performed, is a
constant expression (said array is, after all, assigned a fixed address
at compile, link, or possibly execute time), while a character pointer
variable is *not* a constant expression.  Only constant expressions can
be used to initialize objects of static storage duration. 



More information about the Comp.lang.c mailing list