Topic: type of &array


Author: hpsoar <hpsoar@gmail.com>
Date: Sun, 15 Feb 2009 12:07:13 CST
Raw View
int array[] = {1, 2, 3};
then what is the type of &array? Is it int** ?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Mon, 16 Feb 2009 14:55:07 CST
Raw View
[ This is arguably off-topic for comp.std.c++, but I'm allowing this
post through.  Please do keep any follow-ups topical. -- mod/jad ]

hpsoar wrote:
>
> int array[] = {1, 2, 3};
> then what is the type of &array? Is it int** ?
>


pointer to array of three int.

--
Note that robinton.demon.co.uk addresses are no longer valid.

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Andrew Koenig" <ark@acm.org>
Date: Mon, 16 Feb 2009 14:56:05 CST
Raw View
"hpsoar" <hpsoar@gmail.com> wrote in message
news:d3069292-352c-4b0f-944f-b360e9d40edf@v5g2000pre.googlegroups.com...

> int array[] = {1, 2, 3};
> then what is the type of &array? Is it int** ?

int(*)[3], otherwise known as "pointer to array of 3 int"


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Yechezkel Mett <ymett.on.usenet@gmail.com>
Date: Mon, 16 Feb 2009 14:56:32 CST
Raw View
On Feb 15, 8:07 pm, hpsoar <hps...@gmail.com> wrote:
> int array[] = {1, 2, 3};
> then what is the type of &array? Is it int** ?

int (*)[3]

Yechezkel Mett


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: baltasarq <baltasarq@gmail.com>
Date: Mon, 16 Feb 2009 15:09:59 CST
Raw View
Hi !


> int array[] = {1, 2, 3};
> then what is the type of &array? Is it int** ?

Sure. int[] is actually int *, so the type of &array is the same as
int **.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "jdennett@acm.org" <james.dennett@gmail.com>
Date: Mon, 16 Feb 2009 23:33:53 CST
Raw View
On Feb 16, 1:09 pm, baltasarq <baltas...@gmail.com> wrote:
> Hi !
>
> > int array[] = {1, 2, 3};
> > then what is the type of &array? Is it int** ?
>
> Sure. int[] is actually int *, so the type of &array is the same as
> int **.

That's not correct.  The type of array is int[3], i.e., array of three
ints.  It is not a pointer (though in many contexts its value decays
to a pointer to its first element).

As most responses correctly noted, &array has type int(*)[3], i.e.,
pointer to array of 3 ints.

Considering arrays to be pointers is a common error, possibly confused
by the fact that function declarations allow pointer parameters to be
declared using array syntax.  In the following code, the type of
&pointer really is int**, as pointer is an int*:

void fn(int pointer[3]) {
   int ** indirect = &pointer;
}

but this code should not compile:

void fn() {
   int array[3];
   int ** error = &array;
}

and with the first compier to hand it gives

small.cpp: In function 'void fn()':
small.cpp:3: error: cannot convert 'int (*)[3]' to 'int**' in
initialization

-- James


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Mon, 16 Feb 2009 23:34:24 CST
Raw View
baltasarq wrote:
> Hi !
>
>
>> int array[] = {1, 2, 3};
>> then what is the type of &array? Is it int** ?
>
> Sure. int[] is actually int *, so the type of &array is the same as
> int **.
>
>

No, no, no. I suggest that you confine yourself to asking questions
because you obviously do not know the answers. There are some contexts
in which a pointer to an array decays to a pointer to the first element
but note the 'decays'.



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: baltasarq <baltasarq@gmail.com>
Date: Tue, 17 Feb 2009 12:43:15 CST
Raw View
Hi, there !

> That's not correct.  The type of array is int[3], i.e., array of three
> ints.  It is not a pointer (though in many contexts its value decays
> to a pointer to its first element).

As soon as you abandon the function in which the array is defined, any
possible information about it "is lost", and you can only be sure that
you have a pointer to the first element. That's why I said int[] is
int *, because that's the only thing you can be sure of during the
whole program.

> Considering arrays to be pointers is a common error, possibly confused
> by the fact that function declarations allow pointer parameters to be
> declared using array syntax.  In the following code, the type of
> &pointer really is int**, as pointer is an int*:
>
> void fn(int pointer[3]) {
>    int ** indirect = &pointer;
> }

Well, I don't think that is an error, as I stated above.

> but note the 'decays'.

Well, everyone is noting the word "decays", while ignoring that I said
"is the same as", not just "is".

> No, no, no. I suggest that you confine yourself to asking questions
> because you obviously do not know the answers.

Obviously, my interest in the language is more practical than
theoretical.
I'm sorry I have disturbed the group.

Kind regards,

Baltasar



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Joe Smith" <unknown_kev_cat@hotmail.com>
Date: Tue, 17 Feb 2009 13:03:31 CST
Raw View
"jdennett@acm.org" <james.dennett@gmail.com> wrote:
> That's not correct.  The type of array is int[3], i.e., array of three
> ints.  It is not a pointer (though in many contexts its value decays
> to a pointer to its first element).
>
> As most responses correctly noted, &array has type int(*)[3], i.e.,
> pointer to array of 3 ints.
>
> Considering arrays to be pointers is a common error, possibly confused
> by the fact that function declarations allow pointer parameters to be
> declared using array syntax.  In the following code, the type of
> &pointer really is int**, as pointer is an int*:
>
> void fn(int pointer[3]) {
>   int ** indirect = &pointer;
> }

Nice reply. I think that is probably the best answer to the original
question I've seen yet.

Any which way, this entire thread has been offtopic for this group. Not
only
is this actually a C question (since it was not asking if C++ differed from
C here, and this is most definately part of the common sublanguage), but it
is a question whose answer long predates Standard C, much less Standard C++
C++. Further, the Question was multi-posted, with the original post
occurring in comp.lang.c++, where it received plenty of answers, so there
really is no point in continuing this thread here. If it must continue,
please do so by locating the equivalent thread in comp.lang.c++ and
replying
there, although do note that the thread is still arguably off-topic there
too.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Wed, 18 Feb 2009 14:52:31 CST
Raw View
baltasarq wrote:

>
>> No, no, no. I suggest that you confine yourself to asking questions
>> because you obviously do not know the answers.
>
> Obviously, my interest in the language is more practical than
> theoretical.
> I'm sorry I have disturbed the group.
>
I wish you would give attributions particularly when replying to
multiple posts in a single post.

The type of &array in the context of the original post is not just
theoretical it is of practical importance in C++ where there are
important contexts in which the result does not immediately decay to a
pointer. Here are two minimal examples:

void foo(int (& a)[3])
and

template<typename T>
void foo(T){ std::cout << sizeof T ; }
int array[] = {1, 2, 3};
main(){
    foo(array);
}

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Wed, 18 Feb 2009 14:54:46 CST
Raw View
Joe Smith wrote:
>
> "jdennett@acm.org" <james.dennett@gmail.com> wrote:
>> That's not correct.  The type of array is int[3], i.e., array of three
>> ints.  It is not a pointer (though in many contexts its value decays
>> to a pointer to its first element).
>>
>> As most responses correctly noted, &array has type int(*)[3], i.e.,
>> pointer to array of 3 ints.
>>
>> Considering arrays to be pointers is a common error, possibly confused
>> by the fact that function declarations allow pointer parameters to be
>> declared using array syntax.  In the following code, the type of
>> &pointer really is int**, as pointer is an int*:
>>
>> void fn(int pointer[3]) {
>>   int ** indirect = &pointer;
>> }
>
> Nice reply. I think that is probably the best answer to the original
> question I've seen yet.
>
> Any which way, this entire thread has been offtopic for this group. Not
> only
> is this actually a C question (since it was not asking if C++ differed from
> C here, and this is most definately part of the common sublanguage), but it
> is a question whose answer long predates Standard C, much less Standard C++
> C++. Further, the Question was multi-posted, with the original post
> occurring in comp.lang.c++, where it received plenty of answers, so there
> really is no point in continuing this thread here. If it must continue,
> please do so by locating the equivalent thread in comp.lang.c++ and
> replying
> there, although do note that the thread is still arguably off-topic there
> too.
>
>

No, it is not OT most particularly because the answer is important in
C++ though largely of academic interest in C++. Type information is used
much more extensively in C++ than in C.

It is hard to write a C program where it matters because you have to get
to multidimensioned arrays to discover that there really is an array
type which does not always decay. Oh and there is a classic case of
getting the declaration correct in multiple TUs.

In C++ we have to worry about the distinction because it manifests in
both references and templates.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kuyper <jameskuyper@verizon.net>
Date: Wed, 18 Feb 2009 14:55:59 CST
Raw View
baltasarq wrote:
> Hi, there !
>
>> That's not correct.  The type of array is int[3], i.e., array of three
>> ints.  It is not a pointer (though in many contexts its value decays
>> to a pointer to its first element).
>
> As soon as you abandon the function in which the array is defined, any
> possible information about it "is lost", and you can only be sure that
> you have a pointer to the first element. That's why I said int[] is
> int *, because that's the only thing you can be sure of during the
> whole program.

If you pass the value &array to another function, the type of that
pointer will contain the length information. It would be an error, for
instance, to pass &array to a function declared as taking an argument of
type int(*)[4]. If passed to a template like the following

template <type T, size_t N> size_t array_length(T (*p)[N])
{ return N; }

then array_length(&array) will return a value of 3.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kuyper <jameskuyper@verizon.net>
Date: Wed, 18 Feb 2009 14:55:15 CST
Raw View
Joe Smith wrote:
>
> "jdennett@acm.org" <james.dennett@gmail.com> wrote:
>> That's not correct.  The type of array is int[3], i.e., array of three
>> ints.  It is not a pointer (though in many contexts its value decays
>> to a pointer to its first element).
>>
>> As most responses correctly noted, &array has type int(*)[3], i.e.,
>> pointer to array of 3 ints.
....
> Any which way, this entire thread has been offtopic for this group. Not
> only
> is this actually a C question (since it was not asking if C++ differed from
> C here, and this is most definately part of the common sublanguage),

It's a question about what the type of &array is in C++. The fact that
the answer happens to be the same in C doesn't make this any less of a
C++ question. In addition, anyone sufficiently uncertain of the answer
to bother asking the question is unlikely to be certain that it's the
same unknown answer in C, particularly if he only programs in C++. Why
should he post it anywhere other than to a C++ group?

I'm in agreement with you on the other issues you raised.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jeff Schwab <jeff@schwabcenter.com>
Date: Thu, 19 Feb 2009 11:27:00 CST
Raw View
Francis Glassborow wrote:

> The type of &array
....
> is of practical importance in C++ where there are
> important contexts in which the result does not immediately decay to a
> pointer.

#include <iostream>
> template<typename T>
> void foo(T){ std::cout << sizeof T ; }
> int array[] = {1, 2, 3};
int
> main(){
>    foo(array);
> }

What should happen here?  It seems as though (1) the array should decay,
or (2) there should be a compile-time failure (since the array cannot be
passed by value), or (3) f should not even be considered (due to
SFINAE), resulting in a link-time error.  GCC 4.2.1 decays the array.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Gert-Jan de Vos <gert-jan.de.vos@onsneteindhoven.nl>
Date: Thu, 26 Feb 2009 10:00:13 CST
Raw View
On Feb 19, 6:27 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> Francis Glassborow wrote:
> #include <iostream>
>
> > template<typename T>
> > void foo(T){ std::cout << sizeof T ; }
> > int array[] = {1, 2, 3};
> int
> > main(){
> >    foo(array);
> > }
>
> What should happen here?  It seems as though (1) the array should decay,
> or (2) there should be a compile-time failure (since the array cannot be
> passed by value), or (3) f should not even be considered (due to
> SFINAE), resulting in a link-time error.  GCC 4.2.1 decays the array.

GCC 4.2.1 is correct per 14.8.2.1-2:

If the function template parameter type P is not a reference type:

- If the type of the argument of the call A is an array type, the
pointer type produced by the array-to-pointer conversion (4.2) is used
in place of A for type deduction;


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Keith Thompson <kst-u@mib.org>
Date: Thu, 26 Feb 2009 10:02:26 CST
Raw View
baltasarq <baltasarq@gmail.com> writes:
>> int array[] = {1, 2, 3};
>> then what is the type of &array? Is it int** ?
>
> Sure. int[] is actually int *, so the type of &array is the same as
> int **.

Section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>, does a very
good job of explaining the relationship between pointers and arrays in
C.  Most or all of it applies to C++ as well.

Arrays are not pointers.  Pointers are not arrays.  Inserting the
words "the same as" doesn't change this.

--
Keith Thompson (The_Other_Keith) kst@mib.org  <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"

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]