Not again!

art at ACC.ARPA art at ACC.ARPA
Fri Jul 18 08:32:43 AEST 1986



> What is the difference between:
> extern	char	*A;
> and
> extern	char	A[];
> 
> If you do: printf("A=%s\n",A);
> the first causes a core dump, the second works.
> 
> I thought pointers and arrays were equivalent?
> <*> Fred Hirsch <*> AT&T Bell Laboratories <*> ihnp4!cord!fjh <*>

AARRRGGHH!!! Not this AGAIN!

1) extern	char *A;

	This declares a variable which contains a POINTER which is
	used to reference entities of type CHAR.

2) extern	char A[];

	This declares a variable which contains an ARRAY of entities
	of type CHAR (of unspecified length).

When allocating memory, the first form causes enough memory to be allocated
to hold an address which points to a char sized entity.  The second form causes
enough memory to be allocated to hold a sequence of char sized entities
(the number must be specified by an explicit subscript or an initializer
when storage is being allocated).

When an array name is used in an expression, it is EQUIVALENT to a POINTER
to the FIRST ELEMENT of the array.  The transformations:

	A[i] <--> *(ptr-to-A + i)
		and
	A <--> ptr-to-A

do NOT make arrays and pointers the same thing!

					<Art at ACC.ARPA>

------



More information about the Comp.lang.c mailing list