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


Groups > comp.lang.c > #20049

Re: The question regarding type of pointers

Message-ID <4F9D4101.128@mindspring.com> (permalink)
Date 2012-04-29 09:24 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: The question regarding type of pointers
References <a5d5b150-de6c-4bdf-bc53-5dd21eddc81c@a8g2000pbe.googlegroups.com> <39cgp71somr8bjmg3rufk6ins863nhdcco@4ax.com> <96bb54c9-5ea3-49ff-851b-56dfbb8eac54@qg3g2000pbc.googlegroups.com> <4F9C3E13.41E3@mindspring.com> <ce807310-bc61-4674-8657-05535111bacd@k7g2000pbo.googlegroups.com>

Show all headers | View raw


somenath wrote:
> 
> On Apr 28, 11:59 pm, pete <pfil...@mindspring.com> wrote:
> > 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
> >
> > No, ar[3] doesn't contain any pointer.
> >
> > ar[3] is an expression of type array of three int.
> 
> I did not understand this. According to my understanding C does not
> have array value instead Instead, the "value" of the array is a
> pointer to the first element of that array.
> 
> So for if we have int ar[3];
> ar will have address of ar[0]. Essentially it is pointer to the first
> element of a row  of 3 int.
> So now if we have int ar[3][3];
> 
> ar[0] will have the address of the first element of the the first row
> of 3 int.
> similarly ar[3] will have the address of the first  element of the
> third row of 3 int.
> So the type of ar[3] will be int(*)[3] in value context. Where I am
> going wrong? Please correct my understanding.
> 
> >
> > /* BEGIN new.c */
> >
> > #include <stdio.h>
> >
> > int
> > main(void)
> > {
> >     char ar[3][7];
> >
> >     printf("sizeof ar[3] is %u\n.", (unsigned)sizeof ar[3]);
> >     return 0;
> >
> > }
> 
> So sizeof operator needs ar[3] in object context. So ar[3] in the
> above is in object context not in value context. Now ar[3] is an array
> object of 7 char. So the printf will print the result as sizeof
> (char ) *7 not sizeof(char (*)[7])
> 
> Now if we think about the value of ar[3] then it will be the address
> of the first element of the the third row of size 7.  Please correct
> me if I am wrong.

There is no ar[3], so ar[3] has no value.

This declaration 
    {char ar[3][7];}
declares an array of 3 arrays of 7 char.
The 3 arrarys are ar[0], ar[1], and ar[2].
The size of each of those 3 arrays is 7 bytes.

It doesn't matter that there is no ar[3]
for the expression (sizeof ar[3]) 
because the operand of sizeof is not evaluated.
The type of the expression of the operand
is all that matters to the sizeof operator.

    printf("sizeof ar[3000] is %u\n.", (unsigned)sizeof ar[3000]);
would have outputed 
    "sizeof ar[3000] is 7"

Expressions of array type are converted to pointers
in all but 3 contexts:
1    operand of sizeof operator
2    operand of address operator
3    string literal initializer

When you have a declaration like
    char ar[3][7];
without any more context,
then I will describe the type of the expression (ar),
as an array type.
The type of the expression (ar + 0), is a pointer type.

You can consider the entire contents of an array
as the value of the array.
But, there is no operator in C which can assign
the value of an array to another array,
and there is no operator in C which can compare
the value of one array with another.


-- 
pete

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web