Re: Using &array with scanf
In article <1136468408.906 944.212590@g49g 2000cwa.googleg roups.com>
James Daughtry <mordock32@hotm ail.com> wrote:[color=blue]
>Ah, now that's my problem. I made a beeline to that very paragraph to
>prove my point, and the result was a quickie program much like the
>following. He was trying to tell me through the output of the program
>that array and &array result in the same address, and the type doesn't
>matter because scanf will treat the same address like a pointer to
>char, and the type mismatch is irrelevant.[/color]
The problem with this argument lies in the phrase "the type mismatch
is irrelevant". That may be the case on *his* machine, but it is
definitely not the case on *every* machine.
There are machines (Burroughs A-series) in which integers are stored
in the same format as floating-point. (The compiler just makes sure
that the fractional part is always zero.) On such a machine, if you
do this:
double x;
ret = scanf("%d", (int *)&x);
if (ret == 1)
printf("I got %f\n", x);
the code actually works! But it fails on most machines, because the
type is *not* irrelevant on most machines.
For a machine on which the type matters, find a 1960s era PR1ME, or
perhaps even a 1980s Data General MV/10000 (aka Eclipse). (I am not
sure whether the type "pointer to array N of char" on the Eclipse
uses a word pointer, but if it does, the &array-with-scanf call will
in fact fail.)
[color=blue]
>Unfortunatel y, he's the kind of person who uses the "it works for me"
>argument. I know he's wrong, you know he's wrong, but he refuses to
>admit that he's wrong until I can write a program that proves him
>wrong. :-P[/color]
If he is familiar with basketball, try the argument Steve Summit once
repeated here (I am not sure where he got it):
Someone told me that, in basketball, you have to bounce the
ball off the floor; you can't hold the ball and run around the
court. But I tried it and it works just fine. Obviously he
does not understand basketball!
Or, for Boston drivers, remember the rule about driving the wrong
way on a one-way street ("it's OK as long as you're in reverse!").
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
In article <1136468408.906 944.212590@g49g 2000cwa.googleg roups.com>
James Daughtry <mordock32@hotm ail.com> wrote:[color=blue]
>Ah, now that's my problem. I made a beeline to that very paragraph to
>prove my point, and the result was a quickie program much like the
>following. He was trying to tell me through the output of the program
>that array and &array result in the same address, and the type doesn't
>matter because scanf will treat the same address like a pointer to
>char, and the type mismatch is irrelevant.[/color]
The problem with this argument lies in the phrase "the type mismatch
is irrelevant". That may be the case on *his* machine, but it is
definitely not the case on *every* machine.
There are machines (Burroughs A-series) in which integers are stored
in the same format as floating-point. (The compiler just makes sure
that the fractional part is always zero.) On such a machine, if you
do this:
double x;
ret = scanf("%d", (int *)&x);
if (ret == 1)
printf("I got %f\n", x);
the code actually works! But it fails on most machines, because the
type is *not* irrelevant on most machines.
For a machine on which the type matters, find a 1960s era PR1ME, or
perhaps even a 1980s Data General MV/10000 (aka Eclipse). (I am not
sure whether the type "pointer to array N of char" on the Eclipse
uses a word pointer, but if it does, the &array-with-scanf call will
in fact fail.)
[color=blue]
>Unfortunatel y, he's the kind of person who uses the "it works for me"
>argument. I know he's wrong, you know he's wrong, but he refuses to
>admit that he's wrong until I can write a program that proves him
>wrong. :-P[/color]
If he is familiar with basketball, try the argument Steve Summit once
repeated here (I am not sure where he got it):
Someone told me that, in basketball, you have to bounce the
ball off the floor; you can't hold the ball and run around the
court. But I tried it and it works just fine. Obviously he
does not understand basketball!
Or, for Boston drivers, remember the rule about driving the wrong
way on a one-way street ("it's OK as long as you're in reverse!").
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Comment