ptrs and arrays

Jan Morales/Development janm at eliot.UUCP
Fri Nov 10 06:07:58 AEST 1989


In article <304 at jhereg.Minnetech.MN.ORG> Mark H. Colburn writes:
>In article <2640 at dogie.macc.wisc.edu> Ross Yahnke writes:
>>Is there a more graceful way to do the following? I allocate
>>a chunk 'o' mem and keep a ptr to it, I want to access the
>>mem as an integer array and inc different elements of the array.

If I read Ross' problem correctly, all you really need is:

foo()
{
	int *aPtr;
	char *calloc();

	aPtr = (int *) calloc(NUM_OF_INTS_IN_ARRAY, sizeof(int));
	aPtr[10]++;
}

That last line of code will increment the eleventh integer in the
"chunk" of memory (aPtr[0] being the first).  Keep in mind that you
should check for a NULL pointer from calloc meaining there's not
enough memory for your request.

Jan
-- 
{uunet,pyramid}!pyrdc!eliot!janm



More information about the Comp.lang.c mailing list