SUMMARY OF confusion with char *a and char a[NUM]

Jim Klavetter jjk at jupiter.astro.umd.edu
Thu Dec 6 08:23:44 AEST 1990


Thanks everyone for the end of confusion.  In addition to the typos
(NUM+1 and ":"), it was made abundantly clear to me that you can't use
a "constant" as an lvalue:
	char a[NUM];
	char *b;

	/*  malloc b for NUM things  */
	a=strchr(string, ':');	/*   ERROR  */
	b=strchr(string, ':');  /*   CORRECT   */

Of course I knew that and I shouldn't have had to post to the net.
However, the following is ok in both cases (same declarations as
above):
	strcpy(string, a);
	strcpy(string, b);
Again, it was explained to me that a is "promoted" to type char*.  Or
another way it was put was the usual "equivalence" of pointers and
arrays (and this is what the faq list details).  

I guess my followup question is more theoretical in nature:  is this a
good thing?  Why shouldn't 
	 strcpy(string, a); 
also send an error message since the man page explicitely says that a
(in this case) is char*.  Conceptutually I understand that a copy (in
this case) is fundamentally different than an equivalence (as above
with strchr()), and also that a is unambiguous in this case, but as so
many are willing to point out:  pointers are not arrays!  (and vice
versa).  Thus, the man page is asking for a pointer, but I am giving
it an array, yet it works.

I hope I am making my confusion a little clearer, here.  It is not
that I don't understand the difference between pointers and arrays,
but that the language itself, IN SOME CASES, doesn't seem to think
there is any difference.

I'm certainly not a c basher, it just seems as if this is an
inconsistency.

Thanks once again for all the help, and I hope that this doesn't
produce TOO many flames.

jjk at astro.umd.edu
Jim Klavetter (accepting mail for Athabasca and Reudi)
Astronomy
UofMD
College Park, MD  20742



More information about the Comp.lang.c mailing list