[net.lang.c] Pointer confusion -- forgot original title

david@ukma.UUCP (David Herron, NPR Lover) (02/08/85)

In article <11807@gatech.UUCP>arnold@gatech.UUCP (Arnold Robbins) writes:
>Morris M. Keesan {decvax,linus,ihnp4,wivax,wjh12,ima}!bbncca!keesan writes:
>> 
>>	[.....]
>> 	int *ptr[];            /* ptr is a pointer to an array of int */
>>	[.....]
>>
>
>Sorry, but this declaration means ptr is an array of pointers to ints (similar
>to the char *argv[] declaration of argv).
>
>A pointer to an array of ints would be
>
>	int array[] = { 1, 2, 3 };
>	int *ptr = & array[0];	/* just use a simple pointer */
>	/* or int *ptr = array; but that is what started this whole mess */
...
>I heartily agree that pointers and array are probably the most confusing
>aspect of C.

Sorry, you are wrong.  A "int **ptr" would be an array of pointers to int.
(The array (in this case) the array is of indeterminate length).  Another
way of declaring this is "int *(ptr[<length>])".

In general it is best to help out your compiler whenever possible
by parenthesizing constructs.  But in C, the parenthesizing gets
slightly confusing when declaring arrays of pointers to functions.

(BTW, the original declaration above was an array of pointers).

>Help advance the state of Computer Science: Nuke a PR1ME today!

Hey!  At least you have some sense about you!  :-) :-) :-) :-)
-- 
-:--:-
David Herron;
		ARPA-> "ukma!david"@ANL-MCS or david%ukma.uucp@anl-mcs.arpa
		UUCP-> {ucbvax,unmvax,boulder,research}!anlams!ukma!david
		UUCP-> {mcvax!qtlon,vax135,mddc}!qusavx!ukma!david
		UUCP-> {A-Large-Portion-of-The-World}!cbosgd!ukma!david

No stupid sayings (I can't think of one).

No stupid disclaimers (Nobody else would claim my statements anyway).

annab@azure.UUCP (A Beaver) (02/20/85)

>References: <677@ukma.UUCP>
> In article <11807@gatech.UUCP>arnold@gatech.UUCP (Arnold Robbins) writes:
> >Morris M. Keesan {decvax,linus,ihnp4,wivax,wjh12,ima}!bbncca!keesan writes:
> >>	[.....]
> >> 	int *ptr[];            /* ptr is a pointer to an array of int */
> >>	[.....]
> >Sorry, but this declaration means ptr is ->[an array of pointers to ints]
	 (similar
> >to the char *argv[] declaration of argv).
> >A pointer to an array of ints would be
> >	int array[] = { 1, 2, 3 };
> >	int *ptr = & array[0];	/* just use a simple pointer */
> >	/* or int *ptr = array; but that is what started this whole mess */
> 
> Sorry, you are wrong.  A "int **ptr" would be ->[an array of pointers to int]
> another way of declaring this is "int *(ptr[<length>])".
> -:--:-
> David Herron;
	It has been my understanding, that in USE "*ptr[]" and "**ptr" are
	essentially the same and can be used interchangably.
	However, at initialization you have to declare the size of the array
	as David says above.	The reason that you can get away with
	"char *argv[]", is because the number of arguments that is passed
	to the program environment is know.(in argc)	Am I wrong?

	 Annadiana Beaver
	A Beaver@Tektronix

jsdy@hadron.UUCP (Joseph S. D. Yao) (03/13/85)

> It has been my understanding, that in USE "*ptr[]" and "**ptr" are
> essentially the same and can be used interchangably.
> However, at initialization you have to declare the size of the array
> as David says above.	The reason that you can get away with
> "char *argv[]", is because the number of arguments that is passed
> to the program environment is know.(in argc)	Am I wrong?

This subject has been thouroughly thrashed to death.  If this gets back
to you and you haven't seen an incredibly long article on the subject,
send mail and I will re-post it.  Basically, yes, you are wrong.  An
array of X's declares multiple contiguous units of storage for objects
of type X.  A pointer to X declares one pointer-unit of storage in a
form that can point to an object of type X.  There are a number of
reasons that this distinction is important, primarily that a pointer
is an lvalue and must be initialised (passing an argument counts), while
an array can never be an lvalue.  The only place that the two are really
identical is as an argument to a function.  This is because, for
historical reasons (see previous article), a declaration of an array
argument to a function is IMPOSSIBLE, so when you declare an array, C
says, "oh, he really meant a pointer."   Your argument  w i l l  always
be a pointer -- an array never really gets passed.

jsdy@hadron.UUCP (Joseph S. D. Yao) (03/13/85)

Almost forgot:  so that you can mail me:

Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}