Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!amdahl!pacbell!hoptoad!peora!rtmvax!spec0!blm From: blm@spec0.UUCP (Bharat Lal Madhyani) Newsgroups: comp.lang.c Subject: Re: Pointer Stew Summary: new version of Pointer Stew, due to some typos....thanx. Keywords: Illegal Initialization Message-ID: <387@spec0.UUCP> Date: 5 May 89 19:03:48 GMT References: <386@spec0.UUCP> Organization: Spectrum Technology Group, Alt. Sp. Fl. Lines: 163 Please ignore my previous posting. This is the most recent posting. There were some type-errors in the previous one. Sorry :-) /*************/ /* 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, /* Here is illegal initialization */ 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 defined 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() { while ( **p_path ) printf("%s\n", *p_path++ ); } /***************/ /* program4.c */ /***************/ /* Still consider another program, where I have defined 5 different character pointers, and I am Initializing the *path[] with these pointers, and the program does not compile due to illegal initialization. */ 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 19: illegal initialization "program3.c", line 20: illegal initialization "program3.c", line 21: illegal initialization "program3.c", line 22: illegal initialization "program3.c", line 23: 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 ? -- spec0!blm Bharat Madhyani