pointers to arrays

Pete Peterson rep at genrad.UUCP
Thu Nov 20 02:21:23 AEST 1986


In article <273 at bms-at.UUCP> stuart at bms-at.UUCP (Stuart D. Gathman) writes:
>I am still confused about several things concerning pointers to arrays.
>There does seem to be such a type, even in K & R.
>1) How does one get such an animal?  The only methods I can figure are
>	a) a cast: ( type (*)[] ) array_of_type

		You could declare:
		char *(linesptr)[81]; /*declares linesptr to be a pointer to 81 element
			arrays of chars. */

		Or you could have:
		typedef char line[81];
		typedef line *linept;
		linept linesptr;	/* also declares linesptr to be a pointer to
								81 element arrays of chars */	

>	b) a function returning such a type (but the return must use a cast!)
>2) The SysV semop(2) is defined as:
>		--- deleted references to SysV semop ---
>3) It seems to me that any distinction between a pointer to an array
>   and a pointer to its first element is purely semantic.  (And given
>   the semantic difficult of obtaining a pointer to an array, why use
>   them?)  There is no pointer conversion that I can imagine involved.
>
		Given the declarations above, if you have:
		line page[20];  /* declare an array of 20 "lines" */
		char *lpointer;
		lpointer = linesptr = page;  /* point at first char of first line */
		
		Now if you increment linesptr, you point to the next line
		whereas if you increment lpointer, you point to the next character;
		this is more than a semantic difference.  The distinction is
		admittedly less clear when you have something like "TYPE (*pointer)[]"
		where the size of the array is unspecified so you can't do arithmetic
		on the pointers.
		It doesn't really seem that obtaining the pointer is that difficult.
>
>When are pointers to arrays appropriate?  
>
		In the example above, the difference is apparent; otherwise, it is
		perhaps conceptually clearer if you're dealing with arrays as units
		to have pointers to those units rather than their constituents.


			pete peterson



More information about the Comp.lang.c mailing list