On 2010 Dec 2, at 5:12 PM, Charles Dickman wrote:
On Thu, Dec 2, 2010 at 6:50 PM, Eric Smith <eric at
brouhaha.com> wrote:
Charles Dickman wrote:
Shouldn't it be (i +
sizeof(int)*"He...\n") ?
No. ?There shouldn't be a multiplication by sizeof(anything), because
C does
the scaling automatically, but the automatic scaling has to be of the
size
of the target of the pointer, which is char, not by the size of the
index.
?The automatic scaling is such that indices 0, 1, 2, etc. always
refer to
successive elements of the target type, and not to successive bytes.
Eric
The target size is determined by the declaration of putchar?
No, as has been explained: a[b] == *(a+b); the 'target size' and hence
scaling factor for the integer operand of the addition (be it a or b)
is determined by the base type of the pointer operand.
Problems may be anticipated if the two operands are not constituted
from one integer type (or promotable to integer) and
one pointer type.
In C, indexing with "[]" is really just a syntactic nicety for the sake
of convention, it is not a functional addition to the language.
(I haven't done any C programming to speak of in over 20 years (1988).
I don't think I ever claimed to be a C expert, but I think I can say
people used to treat me as such. I will claim that I answered Eric's
questions correctly when he first posed them.)
--
Way back then, a lowly undergrad learning C came up against a baffling
problem. The program would compile and run but produce inexplicable
results. The instructor couldn't figure it out, so a bunch of grad
students and research assistants were called in, me amongst them. We
stood around and scratched our heads and none of us experts could see
the error. We cognoscenti left, the student did some more debugging and
eventually reported back to us with the explanation.
Perhaps it is more or less obvious when presented in stark, pared-down
form like this, but what is wrong with the following C program, or what
problem might be anticipated? (Arguably, it's not really a C language
problem):
main()
{
Read();
}
Read()
{
blah;
blah;
}