Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Two dimensi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 14 of 32 Topic 26121 of 26821
Post > Topic >>

Re: Two dimensional array Question

by Joe Wright <joewwright@[EMAIL PROTECTED] > May 10, 2008 at 09:26 AM

Ben Bacarisse wrote:
> pete <pfiland@[EMAIL PROTECTED]
> writes:
> 
>> mdh wrote:
>>>>> The exercise associated with this, used the construct
>>>>> *p = arr[1 or 0 ] to point to either the "first" or "second" row of
>>>>> the array. Does the compiler "know" where to point to because it has
>>>>> been given this information by the declaration of "13" in
arr[2][13].
>>>>> Hopefully this makes sense too!
>>>> Yes and yes.
>>>>
>>>  I was in a hurry to get to work this am...so forgot to ask this.
>>> In a multidimensional array, one can simply drop the second []?  as in
>>> *p=arr[i] and this is legal?
>>> Thanks.
>> It's legal regardless of whether or not
>> you understand what you're writing.
>> But it's better if you realize that
>>
>>     *p = arr[i];
>>
>> means the same thing as
>>
>>     *p = &arr[i][0];
> 
> The trouble is this phrase "means the same as".  Both right hand sides
> are certainly closely related expressions, but they have different
> types, different sizes (sizeof arr[i] != sizeof &arr[i][0]) and they
> can't be used in the same places (you can, for example take the
> address of one but not of the other).
> 
> Sometimes it helps to gloss over these differences and sometimes it
> does not.  I am not sure which is the case here.
> 
>> because
>>
>>     &arr[i][0] means &*(*(arr + i) + 0)
>>
>> which can be simplified to
>>
>>     (*(arr + i) + 0)
>>      *(arr + i)
>>        arr[i]
> 
> C is not good for equational reasoning.  The OP might find this
> helpful:
> 
> #include <stdio.h>
> 
> #define PSZ(exp) printf("sizeof " #exp " = %d\n", (int)sizeof exp)
> 
> int main(void)
> {
>      int i = 0;
>      int arr[2][13] = {0};
> 
>      PSZ(&arr[i][0]);
>      PSZ(&*(*(arr + i) + 0));
>      PSZ((*(arr + i) + 0));
>      PSZ(*(arr + i));
>      PSZ(arr[i]);
> 
>      return 0;
> }
> 
All true on the face of it but sizeof was not what was asked, it was 
assignment. Given..

   int *p;
   p = arr[i];

Yes, arr[i] is an array 13 of int but expressing an array as above 
yields the address of its first element, in this case int*.

   p = &arr[i][0];

The address of the int yields the same int*.

-- 
Joe Wright
"Everything should be made as simple as possible, but not simpler."
                     --- Albert Einstein ---
 




 32 Posts in Topic:
Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-08 08:16:13 
Re: Two dimensional array Question
Joe Wright <joewwright  2008-05-08 19:15:37 
Re: Two dimensional array Question
"Default User"   2008-05-08 23:24:55 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-08 21:03:36 
Re: Two dimensional array Question
"Default User"   2008-05-09 16:27:53 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-09 00:30:00 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-08 18:39:27 
Re: Two dimensional array Question
Barry Schwarz <schwarz  2008-05-08 20:54:09 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-09 06:18:51 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-09 20:35:22 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-10 07:09:06 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-10 12:42:22 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-10 08:43:58 
Re: Two dimensional array Question
Joe Wright <joewwright  2008-05-10 09:26:20 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-10 14:53:55 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-10 12:25:49 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-10 17:12:46 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-11 01:17:26 
Re: Two dimensional array Question
Barry Schwarz <schwarz  2008-05-11 04:17:57 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-11 19:07:45 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-12 03:16:04 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-12 03:17:11 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-12 07:18:50 
Re: Two dimensional array Question
"Default User"   2008-05-12 06:19:46 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-12 21:12:17 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-13 04:48:22 
Re: Two dimensional array Question
Chris Torek <nospam@[E  2008-05-13 16:38:01 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-13 16:53:43 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-12 21:13:55 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-16 22:17:14 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-12 22:26:08 
Re: Two dimensional array Question
Barry Schwarz <schwarz  2008-05-16 18:44:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Wed Jul 9 1:12:42 CDT 2008.