Topic: arrays as formal parameters


Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@attglobal.net>
Date: 2000/09/28
Raw View
Am 25.09.00, 19:19:29, schrieb David R Tribble <david@tribble.com> zum
Thema Re: arrays as formal parameters:

> Also:
>           void f( X anX[] );

> If you really want a pointer to an array type, you have to do
> something like:

>     void g(X (*arrX)[3]);

> But it's more cubmersome to use (*arrX)[i] than anX[i].

Of course you can avoid that in C++ by using a reference type:

    void h( X (&arrOfX)[3] );

This can be used as conveniently as 'anX', while it (the signature of h)
is as typesafe as a pointer to array (even safer if you don't consider a
null pointer a legal array).

Of course 'arrOfX' can itself 'decay' to a pointer-to-X where
appropriate. But still typeid(arrOfX) = typeid (X [3]) (instead of
typeid(X*)).

Regards, J   rg

--
J   rg Barfurth                        j barfurth AT vossnet DOT de
-------------- using std::disclaimer; ---------------------------
Software Engineer           mailto:joerg.barfurth@germany.sun.com
Star Office GmbH            http://www.sun.com/staroffice

Join OpenOffice.org     the Open Office Suite at www.Openoffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 2000/09/25
Raw View
James Kuyper wrote:
> [...]  One of the places where arrays decay to pointers is as
> parameters of functions. The following are all equivalent
> declarations:
>
>         void f( X anX[3]);
>         void f( X anX[10]);
>         void f( X *anX);
>
> In all three cases, typeid(anX)==typeid(X*). Therefore, your function
> will take the same kind of argument that you want to pass to it.

Also:
          void f( X anX[] );

If you really want a pointer to an array type, you have to do
something like:

    void g(X (*arrX)[3]);

Now we have:
    arrX         type X (*)[3]
    *arrX        type X[3]
    (*arrX)[0]   type X

But it's more cubmersome to use (*arrX)[i] than anX[i].

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: 2000/09/13
Raw View
Hello,

I know that we all should avoid C-arrays and use vector etc. I also
know that a C-style-array is converted to a pointer in most
situations. So, when I have

  void f( X* )

I can call it for an array of X. and it will receive a pointer to the
first element.

X x[3];
f(x); // OK

But what happens in the case of

void f(  X  anX[3]  );   ?

I still can write f(x). IMO x should also be converted to pointer, but
f wants an array. X* to X[] is no conversion that can be done
implicitely. Why does this work?

Martin






------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/09/13
Raw View

Martin Aupperle wrote:
>
>
> void f(  X  anX[3]  );   ?
>
> I still can write f(x). IMO x should also be converted to pointer, but
> f wants an array. X* to X[] is no conversion that can be done
> implicitely. Why does this work?

No conversion is done.  It is NOT the case that the array name always evaluates
to a pointer to the first element.  In this case the type of f and the type of
the function argument are the same, no conversion is required.

However, unfortuntely, this doesn't really work consistantly with the rest of
the language.  anX becomes a reference to the original array rather than the
array being passed by value as would happen with any other C or C++ data type.

It's pretty much equivelent to your original code that received a pointer except
that the anX has a definite size.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/09/14
Raw View
Martin Aupperle wrote:
>
> Hello,
>
> I know that we all should avoid C-arrays and use vector etc. I also
> know that a C-style-array is converted to a pointer in most
> situations. So, when I have
>
>   void f( X* )
>
> I can call it for an array of X. and it will receive a pointer to the
> first element.
>
> X x[3];
> f(x); // OK
>
> But what happens in the case of
>
> void f(  X  anX[3]  );   ?
>
> I still can write f(x). IMO x should also be converted to pointer, but
> f wants an array. X* to X[] is no conversion that can be done
> implicitely. Why does this work?

The conversion occurs in the declaration of f(), not in the argument
passing. One of the places where arrays decay to pointers is as
parameters of functions. The following are all equivalent declarations:

 void f( X anX[3]);
 void f( X anX[10]);
 void f( X *anX);

In all three cases, typeid(anX)==typeid(X*). Therefore, your function
will take the same kind of argument that you want to pass to it.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]