From: Patrick Scheible on
Charles Richmond <frizzle(a)tx.rr.com> writes:

> Peter Flass wrote:
> > Walter Bushell wrote:
> >> In article <20100305171635.e538ef18.steveo(a)eircom.net>,
> >> Ahem A Rivet's Shot <steveo(a)eircom.net> wrote:
> >>
> >>> On Fri, 5 Mar 2010 09:07:31 -0800 (PST)
> >>> Quadibloc <jsavard(a)ecn.ab.ca> wrote:
> >>>
> >>>> On Feb 26, 4:56� am, Ahem A Rivet's Shot <ste...(a)eircom.net> wrote:
> >>>>
> >>>>> � � � � No, he's saying that C doesn't really implement an
> >>>>> array type,
> >>>>> the var[offset] syntax is just syntactic sugar for *(var + offset)
> >>>>> which is why things like 3[x] work the same as x[3] in C.
> >>>> Um, no.
> >>>>
> >>>> x = y + 3 ;
> >>>>
> >>>> in a C program will _not_ store in x the value of y plus the contents
> >>>> of memory location 3.
> >>> No but x = *(y + 3) will store in x the contents of the memory
> >>> location at 3 + the value of y just as x = y[3] will and x = 3[y] will,
> >>> which is what I stated. You missed out the all important * and ()s.
> >>
> >> No, that will compare x and the right val.
> >>
> >> = is a comparasion operator in c.
> >>
> >
> > '=' is assignment, '==' is comparison.
>
> I think there is *not* a single C programmer who has *not* had his
> hand slapped by making the mistake of using "=" when he meant
> "==".

More than once...

-- Patrick
From: Dennis Ritchie on

"Quadibloc" <jsavard(a)ecn.ab.ca> wrote in message
news:91a047d3-ee98-40b6-876e-9a7221168d5b(a)33g2000yqj.googlegroups.com...

> The 2[a] syntax actually *works* in C the way it was described? I am
> astonished. I would expect it to yield the contents of the memory
> location a+&2 assuming that &2 can be persuaded to yield up the
> location where the value of the constant "2" is stored.

> Evidently there is some discrepancy between C and FORTRAN.

Yes, it really does work as described, and was indeed documented
in the earliest C, and for that matter B and BCPL manuals.
C and Fortran are discrepant here.

Dennis


From: Uwe Kloß on
Quadibloc schrieb:
> On Mar 5, 12:44 pm, Joe Pfeiffer <pfeif...(a)cs.nmsu.edu> wrote:
>> #include <stdio.h>
>> int main()
>> {
>> int a[4];
>>
>> printf("a[2] at 0x%8x\n", &(a[2]));
>> printf("2[a] at 0x%8x\n", &(2[a]));
>> printf("(a+2) is 0x%8x\n", a+2);
>> printf("(2+a) is 0x%8x\n", 2+a);
>>
>> }
>>
>> [pfeiffer(a)snowball ~/temp]# ./awry
>> a[2] at 0xbfff97b8
>> 2[a] at 0xbfff97b8
>> (a+2) is 0xbfff97b8
>> (2+a) is 0xbfff97b8
>
> The 2[a] syntax actually *works* in C the way it was described? I am
> astonished. I would expect it to yield the contents of the memory
> location a+&2 assuming that &2 can be persuaded to yield up the
> location where the value of the constant "2" is stored.

You can think of the "a" in "a[4]" as a named numerical (integer)
constant (alias), giving the address of the memory block that was
allocated by the definition.

So there is no difference, between using that (named) constant or an
explicit numerical constant.

The only differences between:
(1) int a[4];
and:
(2) int * a = malloc( 4 * sizeof(int));
is the place where the memory is allocated and the value in (2) may be
changed later. (And the amount of typing, ofcourse!)

In both cases you can use "a[1]" or "*(a+1)" for access.

> Evidently there is some discrepancy between C and FORTRAN.
Only "a tiny bit"! ;-)

Gr��e,
Uwe
From: Ahem A Rivet's Shot on
On Sat, 06 Mar 2010 15:03:52 -0500
Walter Bushell <proto(a)panix.com> wrote:

> No, that will compare x and the right val.
>
> = is a comparasion operator in c.

Bzzzt wrong!

--
Steve O'Hara-Smith | Directable Mirror Arrays
C:>WIN | A better way to focus the sun
The computer obeys and wins. | licences available see
You lose and Bill collects. | http://www.sohara.org/
From: Ahem A Rivet's Shot on
On Sat, 6 Mar 2010 02:01:30 -0800 (PST)
Quadibloc <jsavard(a)ecn.ab.ca> wrote:

> The 2[a] syntax actually *works* in C the way it was described? I am
> astonished. I would expect it to yield the contents of the memory
> location a+&2 assuming that &2 can be persuaded to yield up the
> location where the value of the constant "2" is stored.

Yes of course it does - why else would I have mentioned it in my
first post in this threadlet ? a is a pointer, 2 is in a integer and 2[a]
is the same as a[2] is the same as *(a+2) and the rules for adding pointers
and integers are well defined in C. This is the heart of my original point,
array notation in C is syntactic sugar for pointer arithmetic (and also
for allocation which I neglected to mention in my original post).

--
Steve O'Hara-Smith | Directable Mirror Arrays
C:>WIN | A better way to focus the sun
The computer obeys and wins. | licences available see
You lose and Bill collects. | http://www.sohara.org/