C declarations (with examples)

karen at vaxwaller.UUCP karen at vaxwaller.UUCP
Wed Feb 6 07:55:55 AEST 1985


> >    I have a question about C declarations.  The [] notation is equivalent
> > to the * notation, right?
>
>% cat > test1.c <<EOF			|	% cat > test2.c <<EOF
>main() {				|	char	x[];
>	char	y[];			|	main()
>	y = "abc";			|	{
>}					|	}
>EOF					|	EOF
>% cc test1.c				|	% cc test2.c
>"test1.c", line 3: illegal lhs of	|	Undefined:
>assignment operator			|	_x
>
>   In test1, the compiler tells us that you can't change the value of the
>identifier which indicates the start of an array.  No matter that they array
>has no elements -- it just won't permit it.  Otherwise, a programmer could
>lose track of his array.  In test2, the compiler assumes that the (evidently)
>null array 'x' must be declared in some other load module; when it's not found,
>the loader complains.
>...
>it won't let you risk losing all references to a block of allocated memory.
>Seems like a good idea to me.
>
>Ray Lubinsky		     University of Virginia, Dept. of Computer Science

it is true that you can't change the value of the identifier which indicates
the start of the array, but i disagree as to why.  if you compile into 
assembly language ("cc" with "-S" on unix) the assembly explains things well.
the following example shows that:  

	1.  the compiler doesn't care if i lose all reference to "ppp",

and	2.  "a" has no contents; it is only an address, whereas "p" has
	    assignable space in addition to the data it points to.

	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
c code:
	char	*p = "ppp";
	char	a[] = "aaa";
	
	main ()
	{
		p = a;
	}
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
assembly:	(...)
		.globl	_p
	_p:
		.data	2		; p gets allocated space
	L18:				;    for a pointer
		.ascii	"ppp\0"		; plus what it points to
		.data
		.long	L18
		.data
		.globl	_a
	_a:				; a is merely an address
		.long	0x616161	;    pointing to its data
		(...)
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

			- karen maleski
			{ucbvax!zehntel, amd}!varian!karen



More information about the Comp.lang.c mailing list