Talk About Network

Google





Programming > C - C++ Learning > Re: [C] Pointer...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 24 Topic 4140 of 4400
Post > Topic >>

Re: [C] Pointer to an array

by Keith Thompson <kst-u@[EMAIL PROTECTED] > May 14, 2008 at 01:10 PM

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"
 




 24 Posts in Topic:
[C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 15:14:02 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 16:30:48 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-14 17:44:04 
Re: [C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 18:15:40 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 18:32:05 
Re: [C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 18:36:49 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 18:55:56 
Re: [C] Pointer to an array
Keith Thompson <kst-u@  2008-05-14 13:10:50 
Re: [C] Pointer to an array
Andrey Tarasevich <and  2008-05-14 11:41:42 
Re: [C] Pointer to an array
Anand Hariharan <znvyg  2008-05-21 04:44:12 
Re: [C] Pointer to an array
"Alf P. Steinbach&qu  2008-05-21 06:38:33 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-21 06:14:08 
Re: [C] Pointer to an array
Barry Schwarz <schwarz  2008-05-25 10:44:26 
Re: Pointer to an array
Anand Hariharan <mailt  2008-05-21 07:50:59 
Re: Pointer to an array
"Alf P. Steinbach&qu  2008-05-21 17:11:16 
Re: Pointer to an array
Bart van Ingen Schenau &l  2008-05-21 19:39:27 
Re: Pointer to an array
Andrey Tarasevich <and  2008-05-21 11:50:21 
Re: Pointer to an array
Anand Hariharan <mailt  2008-05-22 13:51:12 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-26 16:57:06 
Re: [C] Pointer to an array
Francis Glassborow <fr  2008-05-26 19:39:07 
Re: [C] Pointer to an array
Keith Thompson <kst-u@  2008-05-26 14:14:19 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-27 04:01:45 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-27 04:23:15 
Re: [C] Pointer to an array
Andrey Tarasevich <and  2008-05-14 11:32:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Fri Jan 9 5:29:05 PST 2009.