ralph <
nt_cons...@yahoo.net> writes:
> On Sat, 17 Dec 2011 13:55:44 -0800, Keith Thompson <
ks...@mib.org>
> wrote:
>
>>ralph <
nt_cons...@yahoo.net> writes:
>>[...]
>>> I have trouble understanding the constant confusion between arrays and
>>> pointers. Way back when (and we are talking many moons ago <g>) when I
>>> started out in C my mentor explained it very simply in these terms in
>>> the first couple of days.
>>>
>>> 1) There is a difference between declarations and definitions.
>>> Remember which you are dealing with and when.
>>> 2) A pointer is a "variable", an array is a "constant"
>>
>>That doesn't really make sense. An array variable is not a constant:
>>
>> int arr[10];
>> arr[5] = 42;
>>
>>I suppose what it means is that an array name, when it appears in an
>>expression (in most but not all context) decays to a pointer to the
>>array's first element, and that pointer value cannot be modified. But I
>>think that saying an array is a constant just reinforces the confusion
>>between arrays and pointers.
>>
>
> I disagree.
>
> Once the beginner gets it through his head that you can never do an
> assignment to 'arr' you resolve a ton of the questions we get around
> here. That as close to the definition of a "constant" as you can get.
That's not what the word "constant" means, though. It's a bit closer to
what "const" means ("constant" and "const" are very different concepts
in C).
It's true that you can't assign to an array, and just saying that is
probably enough for a beginner. But eventually you're going to want to
understand *why* you can't assign to an array -- and the reason is that
the array name is converted to a pointer to the first element. That
pointer is a *value*, not an object/variable, and so you can't assign to
the pointer either, any more than you can do "42 = 43;".
>>> 3) The array name evaluates to the address of the first element in the
>>> array.
>>
>>This applies to any expression of array type, not just the name of a
>>declared array object. But it doesn't apply in all contexts.
>>Specifically, an expression of array type is converted to a pointer to
>>the array object's first element *unless* it's:
>>
>> The operand of a unary "sizeof" operator (sizeof arr gives you the
>> same of the array, not of a pointer);
>>
>> The operand of a unary "&" operator (&arr gives you the address of
>> the array object; it's the same memory location as the address of
>> its first element, but it's of a different type);
>>
>> A string literal in an initializer used to initialize an array
>> object. In
>> char arr[6] = "hello";
>> the *value* of "hello" is copied into arr.
>>
>
> Yep. But what's the big deal.
The big deal is that that's how the language works. And the big deal is
that it's important to understand that "sizeof arr" gives you the size
of the array itself. Your explanation that "The array name evaluates to
the address of the first element in the array" implies that "sizeof arr"
would yield the size of a pointer.
> And of course I disagree that it actually matters to the beginner that
> the address is of a "different type". Semantically and syntactically
> it comes out the same. Plenty of time to get into vague language
> later.
It *should* matter. The difference in type between a pointer to an int
and a pointer to an array of int might not be the first thing you need
to learn, but you certainly should learn it at some point.
> Actually in some implementations whether "hello" gets copied and when
> if gets copied if it does depends on context. Again, why add
> confusion?
I'm trying to add understanding. The underlying concepts are actually
fairly straightforward, but there are special-case rules involving
arrays and pointers (the decay of array expressions and the adjustment
of array parameters to pointer parameters) that, while convenient, cause
confusion. IMHO, trying to each arrays and pointers in terms of the way
those special-case rules make them *look*, plants the seeds of further
confusion down the line.
I've seen far too many people saying that arrays are "really" just
pointers.
[...]
>>> 6) Since an array name is an address it can be used as a pointer that
>>> is initialized to point to the first element in the array.
>>
>>In most contexts; see above. It would be clearer to say that an array
>>name *decays to* an address (or pointer value). And I wouldn't use the
>>word "initialized".
>>
>
> I would. And again what does 'decay' meant to the beginner?
> In code the name "arr" is changed to, or evaluates to, the address of
> the first element.
Ok, so don't use the word "decay". The standard doesn't; it describes
it as a conversion (and perhaps I should have as well). The term
"decay" is commonly used to describe the process, though.
[...]
>>> 9) The array operator evaluates to a storage map equation, eg.
>>> type ar[i] := *(a + (i * sizeof(type))
>>
>>I'm not sure what syntax you're using. What does "type" mean here? And
>>C pointer arithmetic is already scaled by the size of the pointed-to
>>type.
>>
>>arr[i] is by definition equivalent to *(arr + i).
>>
>
> Think about it.
I have, at great length.
Again, can you explain what you mean by "type"? The equation makes more
sense without it.
Most of your equation, apart from the ":=", is valid C syntax:
ar[i] := *(a + (i * sizeof(type))
which tends to imply that you're using the "+" operator as defined by C.
But in fact, you're using a "+" operator that adds a specified number of
*bytes* to a pointer value; that's not how C's "+" operator works.
It's certainly worth explaining that the addition is scaled by the size
of the pointed-to type, but using a C expression that doesn't have the
semantics you're assuming is not a good way to do it.
> You say that now, but you come back later in other response to point
> out "sizes". That "already scaled" is because the compiler knows the
> type, and he knows the type because you declared it.
I'm not aware that I've written anything that contradicts what I'm
saying here. If I have, please point it out.
[snip]
Incidentally, when posting a followup, please feel free to snip any
quoted material that you're not responding to.