[net.lang.c] array refs

cottrell@nbs-vms.ARPA (01/11/85)

/*
i disagree about referencing arrays 'out of bounds'. i have often taken
a pointer, moved it down the array, when suddenly i find i have to
pervert the previous character. what do i do? something like: p[-1] = ... ;
i see nothing wrong with negative indexing, altho i dont believe in
floating point :-)
also: about the construct:
	for(p = buf; p < &buf[SIZE]; p++)
someone said it breaks at the wraparound point of memory. true, but lotsa
weird stuff happens there. i know p <= &buf[SIZE - 1] works there, but
this is a pathological case. most programs deal with address space that
look like 0 < lo < hi < wrap.
also also: another use of array refs beyond the 'bounds' is a struxure
that ends with a variable size string:

	struct weird {
		int	count;
		...
		...
		char	string[1];
	};

	w = malloc(sizeof(struct weird) + strlen(source);
	w->count = strlen(strcpy(w->string,source));

howzabout them apples?
*/

cogito@tjalk.UUCP (Robbert van Renesse) (01/13/85)

In article <7225@brl-tgr.ARPA> cottrell@nbs-vms.ARPA writes:
>/*
>i disagree about referencing arrays 'out of bounds'. i have often taken
>a pointer, moved it down the array, when suddenly i find i have to
>pervert the previous character. what do i do? something like: p[-1] = ... ;

I see nothing wrong with negative indexing either, on the contrary, it
is very useful when you want to have all the arguments of main in one
string:

	main(argc, argv)
	char *argv[];
	{
		register i;

		for (i = 2; i < argc; i++)
			argv[i][-1] = ' ';
		do_it(argv[1]);
	}

What did you say? Portability? Don't be a bore.
-- 
			Robbert van Renesse
			...!{decvax|philabs|seismo}!mcvax!vu44!cogito

jsdy@SEISMO.ARPA (01/29/85)

> 	struct weird {
> 		int	count;
> 		...
> 		...
> 		char	string[1];
> 	};
> 
> 	w = malloc(sizeof(struct weird) + strlen(source);
> 	w->count = strlen(strcpy(w->string,source));

While I know that this is sometimes necessary when dealing with external
communications devices (e.g.), I've never really been happy about being
forced to use this device.  It seems, somehow, to be betraying the old
and good principle of "saying what you  m e a n ".

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