Why are character arrays special (equal rights for structs)

Walker Mangum walker at ficc.uu.net
Wed Feb 8 08:37:40 AEST 1989


In article <15833 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
> In article <19742 at uflorida.cis.ufl.EDU> thoth at beach.cis.ufl.edu
> From: chris at umcp-cs.UUCP (Chris Torek)
> 
> In article <138 at darth.UUCP> gary at darth.UUCP (Gary Wisniewski) writes:
> >As far as your question about "char *help[]" and "char **help": the two
> >forms are IDENTICAL to virtually every C compiler (that's worth its
> >salt).  Arrays in C are merely special cases of pointers.  In other
> >words, both forms are correct.
> 

[ K&R quotes deleted ]

> 	char	msg0[] = "Hello, world";
> 	char	*msg1 = "Hello, world";
> 
> Given both declarations,
> 
> 	printf("%s\n", msg0);
> 
> and
> 
> 	printf("%s\n", msg1);
> 
> produce the same output.  Yet msg0 and msg1 are not the same:
> 
> 	printf("%d %d\n", sizeof (msg0), sizeof (msg1));
> 

An important difference is, for any C compiler "that's worth its salt",
msg0 may not *not* be used as an lvalue!

Try this on your compiler:

char	msg0[] = "Hello, world";
char	*msg1 = "Hello, world";

main(argc,argv)
int argc;
char *argv[];

{
    msg1 = msg0;   /* this is ok - msg1 is a pointer, a legal "lvalue"		 */
    msg0 = msg1;   /* this better fail if your compiler is "worth its salt"! */
                   /* msg0 is the address of an array, and may not be        */
                   /* reassigned.  In K&R terms, it may be used only as an   */
				   /* "rvalue", not an "lvalue"								 */
}

I get the following:
x.c(10) : error 106: `=' : left operand must be lvalue


-- 
Walker Mangum                                  |  Adytum, Incorporated
phone: (713) 333-1509                          |  1100 NASA Road One  
UUCP:  uunet!ficc!walker  (walker at ficc.uu.net) |  Houston, TX  77058
Disclaimer: $#!+ HAPPENS



More information about the Comp.lang.c mailing list