Aggro <spammerdream@[EMAIL PROTECTED]
> writes:
> Richard Heathfield wrote:
>> Aggro said:
>>
>>> Ben Bacarisse wrote:
>>>
>>>> The second case is the "normal" one so I assume you know why that one
>>>> is fine! (The name of the array -- an array-valued expression -- is
>>>> converted to a pointer to its first element and then that is
converted
>>>> to a void * for memset.)
>>> So both usr_data and &usr_data are converted into &usr_data[0] by the
>>> compiler?
>> No. usr_data is converted to &usr_data[0], a char *. &usr_data
>> undergoes no such conversion, and is a char (*)[128]. The difference
>> between them is pretty much the same as the difference between 0 and
>> 0.0 - i.e. different types.
>
> OK, that is clear now.
>
>>> If so, then it makes sence. I had seen only syntax usr_data
>>> and &usr_data[0] to be used, so I didn't know one could use also
>>> &usr_data to point the first item in array.
>> That's the mistake I'm trying to steer you away from. &usr_data
>> doesn't point to the first item in the array. It points to the array.
>
> OK. So they have different types, but still the memory address they
> both point to is the same. Which is why the functionality of the
> program is the same. Right?
Right.
&usr_data is a pointer to array of 128 char (type ``char(*)[128]'').
usr_data is of type array of 128 char, and is immediately converted
(as array expressions are in most contexts) to a pointer to the first
element of the array, yielding a value of type char*.
So both expressions are of pointer type, and both point to objects
that start at the same memory address, but they're of different types.
Now, because both expressions are passed as the first argument to
memset(), for which the first parameter is of type void*, both
expressions are converted to void*. One is converted from
``char(*)[128]'' to ``void*'', the other is converted from
``char*'' to ``void*''. Due to a special rule for type void*, the
conversion is done implicitly.
Both conversions to void* yield the same result.
You can think of a conversion of a pointer to void* as discarding any
information about the target type, while keeping information about
where in memory it points.
--
Keith Thompson (The_Other_Keith) kst-u@[EMAIL PROTECTED]
<http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


|