Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19882 > unrolled thread

The question regarding type of pointers

Started bysomenath <somenathpal@gmail.com>
First post2012-04-24 21:05 -0700
Last post2012-04-29 07:32 -0400
Articles 1 on this page of 21 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-24 21:05 -0700
    Re: The question regarding type of pointers Kaz Kylheku <kaz@kylheku.com> - 2012-04-25 04:51 +0000
    Re: The question regarding type of pointers "Heinrich Wolf" <invalid@invalid.invalid> - 2012-04-25 07:15 +0200
      Re: The question regarding type of pointers Philip Lantz <prl@canterey.us> - 2012-04-25 00:46 -0700
        Re: The question regarding type of pointers "Heinrich Wolf" <invalid@invalid.invalid> - 2012-04-25 09:52 +0200
    Re: The question regarding type of pointers Philip Lantz <prl@canterey.us> - 2012-04-25 01:09 -0700
    Re: The question regarding type of pointers Barry Schwarz <schwarzb@dqel.com> - 2012-04-25 11:22 -0700
      Re: The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-27 23:05 -0700
        Re: The question regarding type of pointers pete <pfiland@mindspring.com> - 2012-04-28 14:59 -0400
          Re: The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-28 20:27 -0700
            Re: The question regarding type of pointers James Kuyper <jameskuyper@verizon.net> - 2012-04-29 07:37 -0400
              Re: The question regarding type of pointers pete <pfiland@mindspring.com> - 2012-04-29 09:34 -0400
                Re: The question regarding type of pointers James Kuyper <jameskuyper@verizon.net> - 2012-04-29 11:54 -0400
            Re: The question regarding type of pointers pete <pfiland@mindspring.com> - 2012-04-29 09:24 -0400
              Re: The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-29 07:38 -0700
                Re: The question regarding type of pointers James Kuyper <jameskuyper@verizon.net> - 2012-04-29 12:01 -0400
                  Re: The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-29 09:29 -0700
      Re: The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-27 22:22 -0700
        Re: The question regarding type of pointers Barry Schwarz <schwarzb@dqel.com> - 2012-04-28 20:14 -0700
          Re: The question regarding type of pointers somenath <somenathpal@gmail.com> - 2012-04-28 22:17 -0700
            Re: The question regarding type of pointers James Kuyper <jameskuyper@verizon.net> - 2012-04-29 07:32 -0400

Page 2 of 2 — ← Prev page 1 [2]


#20046

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-04-29 07:32 -0400
Message-ID<jnj8t5$jft$1@dont-email.me>
In reply to#20036
On 04/29/2012 01:17 AM, somenath wrote:
> 
>>> Now let us focus on the two dimensional array.
>>> ==========================================================
>>> Suppose we have the following declaration
>>> int �ar[3][3];
>>
> 
>>
>>> Question=> What is sizeof( ar[3])?
>>
>>> So what is ar[3]? ar[3] contains the pointer �to first element of row
>>> of int where the row size is 3. The type of ar[3] is �int(*)[3]. So
>>
>> ar[3] does not exist. �The only elements of ar are ar[0], ar[1], and
>> ar[2]. �No ar[i] contains any pointer value at all. �Each ar[i] is an
>> array of 3 int. �Period. �No pointers whatsoever.
>>
> I am not able to correlate between the statements which say "C does
> not have array values.Instead, the �value� of the array is a pointer
> to the first element of that array." ...

That last sentence is incorrect. The "value of an array" is a
meaningless concept in C, so all statements about such a thing are
meaningless. What that sentence is trying and failing to correctly
describe is the fact that an lvalue expression with an array type is
automatically converted, in MOST contexts, into a pointer to the first
element of that array. Note that this is a conversion set up by the
compiler, and performed by your program when needed; it is NOT the
retrieval of a saved pointer value.

The exceptions are when applying the unary & operator, the sizeof
operator, and when a string literal is used to initialize an array. In
each of those exceptions, something is done using the entire array - but
the thing that is being done does not involve the non-existent value of
the array: &array gives a pointer to the array, The expression "sizeof
array" gives the size, in bytes, of the array. In the definition

	char array[] = "string literal";

the elements of the array created by the string literal are copied into
the array created by that definition. In many contexts, that copying can
be optimized away, and there will actually be only one array, not two.
However, that's just an optimization - the standard describes the
behavior in terms of having two separate arrays.

> ...with the fact " No ar[i] contains
> any pointer value at all.  Each ar[i] is an  array of 3 int.  Period.
> No pointers whatsoever."

That's right, there are no pointers contained in ar[i]. When used in
various contexts, the expression ar[i] gets converted into a pointer to
the first element of ar[i]; but that pointer is (modulo optimizations)
calculated from the address of 'ar' and the value of 'i'.

> Where I am going wrong? As I have mentioned earlier my understanding
> is ar[i] will have the address of the first element of  the ith row.
> Is this not correct? I am very confused. Please help me to understand
> it correctly.

It is NOT correct. ar[i] does NOT contain a pointer to the members of
ar[i], it only contains those members themselves. Your program knows
precisely where these members are, so it can create a pointer value
pointing at the first one. It knows this, because it knows where ar[i]
is. In turn, it knows where ar[i] is because it knows where ar is.

The details of how that is done are implementation-specific, but on many
systems, your program knows where 'ar' is because there's a register
which has been reserved to store the address of a block of memory which
may contain many variables, including 'ar', each at a fixed offset from
the start of the block. On such a system, when you write ar[i] in a
context where it gets converted into a pointer to the first element of
ar[i], your program performs a calculation equivalent to the following:

	(int*)(base_address + offset_of_ar) + i*3

The value of offset_of_ar is a constant, built into your program. As a
result, the only pointer your program actually needs to keep track of in
order to create a pointer value from the expression ar[i], is the
base_address which is stored in a register.
-- 
James Kuyper

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web