Pointer Stew
Bharat Lal Madhyani
blm at spec0.UUCP
Sat May 6 02:22:24 AEST 1989
/*************/
/* program1.c*/
/*************/
/*
consider the following small c_program
path : is an array of pointers to characters,
which has been initialized
p_path : is a pointer to path, i.e it is a
pointer to pointer to character
The program basically travels the array path and prints the character string
associated with the pointer at that time.
*/
char *path[] = {
"/usr/pgmr/blm/citoh/shit",
"/usr/pgmr/junk",
"/usr/lib/news/history",
"/usr/bin/vi",
"Testing Initialization",
0
};
char **p_path = path ; /* pointer to path */
main()
{
while ( **p_path )
printf("%s\n", *p_path++ );
}
/***************/
/* program2.c */
/***************/
/*
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.
*/
char *bad_address = "Testing Initialization";
char *path[] = {
"/usr/pgmr/blm/citoh/shit",
"/usr/pgmr/junk",
"/usr/lib/news/history",
"/usr/bin/vi",
bad_address,
0
};
char **p_path = path ; /* pointer to path */
main()
{
while ( **p_path )
printf("%s\n", *p_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.
*/
char a1[]="/usr/pgmr/blm/citoh/shit";
char a2[]="/usr/pgmr/junk";
char a3[]="/usr/lib/news/history";
char a4[]="/usr/bin/vi";
char bad_address[] = "Testing Initialization";
char null[]="" ;
char *path[6] = {
a1,
a2,
a3,
a4,
bad_address,
null
};
char **p_path = path ; /* pointer to path */
main()
{
/*path[0] = a1, path[1] = a2, path[2]=a3, path[3]=a4 ;*/
while ( **p_path )
printf("%s\n", *p_path++ );
}
/***************/
/* program4.c */
/***************/
/*
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.
*/
char *a1="/usr/pgmr/blm/citoh/shit";
char *a2="/usr/pgmr/junk";
char *a3="/usr/lib/news/history";
char *a4="/usr/bin/vi";
char *bad_address = "Testing Initialization";
char *null="" ;
char *path[6] = {
a1,
a2,
a3,
a4,
bad_address,
null
};
char **p_path = path ; /* pointer to path */
main()
{
while ( **p_path )
printf("%s\n", *p_path++ );
}
/* I am getting the following errors:
"program3.c", line 18: illegal initialization
"program3.c", line 19: illegal initialization
"program3.c", line 20: illegal initialization
"program3.c", line 21: illegal initialization
"program3.c", line 22: illegal initialization
"program3.c", line 24: illegal initialization
*/
Questions :
. What subtle difference program3.c & program4.c has ?
. 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 ?
--
blm!spec0
Bharat Madhyani
More information about the Comp.lang.c
mailing list