Address of array

Gregory Smith greg at utcsri.UUCP
Tue Apr 1 05:49:52 AEST 1986


This is a repost - sorry if you've seen it, but it 'bounced', so I'm
sending it again. ( How can news bounce? The stuff attached to the returned
news ( presumably explaining the reason ) was greek to me... )

In article <530 at umcp-cs.UUCP> chris at umcp-cs.UUCP (Chris Torek) writes:
>In article <2377 at utcsri.UUCP> greg at utcsri.UUCP (Gregory Smith) writes:
>
>>	char (*char_ar_ptr)[80];
>>
...[ a pointer to array of char ]
>>
>>	char line[80];	/* this is what I want to point it at */
>
>>	char_ar_ptr = &line;		/* this should work ... */
>
>Debatable.
	( obviously! )
>
>This works, and is probably even what you `really' mean:
>
>	#define N	2	/* e.g. */
>	char lines[N][80];
>
>	char_ar_ptr = lines;
>
>If you only have one line, why do you need to point at a set of
>lines?
Good point ... I actually hadn't thought of it exactly that way.

Two answers come to mind, though:

	(1) Because it is there. :-)

	(2) Suppose the array 'lines' is actually more than two:

		char lines[10][80];

	   Suppose I want to point char_ar_ptr at lines[i]
           ( a perfectly reasonable thing to want to do ):

		This is allowed:
		   char_ar_ptr = lines + i;
				/* let's really confuse those novices! >:-) */

		This isn't:
		   char_ar_ptr = &lines[i];  /* pointer to the ith array */

So &a[b] is not equivalent to a+b here. What a shame. I like using
&a[b] in general, and ALWAYS use it when 'a' is an array as opposed to a
pointer. Too bad I can't... especially when there's no good reason for not
being able to.
-- 
"If you aren't making any mistakes, you aren't doing anything".
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list