C question -- pointer to array of characters

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Oct 19 19:29:40 AEST 1989


In article <6569 at ficc.uu.net> kunkee at ficc.uu.net (randy kunkee XNX MGR) writes:
>	char (*foo)[];
>	char bar[20];
>	foo = bar;
>Does not work (well, it works, but the compiler complains).
>Is my C compiler broken?

No.  There are (at least) three problems in this code:
    (1) bar is the same in the assignment expression as &bar[0],
	i.e. a char*, which is not compatible with foo.
    (2) foo has an incomplete type -- it would be impossible for
	the compiler to generate code to perform pointer arithmetic
	involving foo, because the size of the object to which foo
	points has not been specified.
>To put it another way, is there a declaration of "bar" that will
>make the above assignment compile silently, and which allocates
>storage for characters?
    (3) there are five ways to allocate storage in C:
	(a) string literals
	(b) static variables
	(c) auto variables
	(d) function arguments
	(e) invocation of malloc()/realloc()/calloc()
	execution of an assignment expression is not a way to
	allocate storage.



More information about the Comp.lang.c mailing list