Re: Passing pointer to array of structs
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Tue, 25 Mar 2008 11:36:30 -0500
On Tue, 25 Mar 2008 19:21:57 +0300, "Vladimir Grigoriev"
<vlad.moscow@xxxxxxx> wrote:
"Victor Bazarov" <v.Abazarov@xxxxxxxxxxxx> wrote in message
news:fsb0vt$qd8$1@xxxxxxxxxxxxxxxxxx
goodTweetieBird wrote:
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
foo(&a);
}
For arrays foo( &a ) is equivalent for foo( a ).
In value but not in type. Above, the type of &a is int(*)[10], and assuming
the function foo is declared foo(int*), the "a" in foo(a) undergoes the
array to pointer conversion resulting in a pointer to a's first element of
the type int*. In English, these types are "pointer to an array of 10 ints"
and "pointer to an int", respectively. To see the difference, consider what
it means to increment them.
The difference exists only
if 'a' was declared as a pointer to an array not array itself.
For example
char a[2] = "A";
and
char * a = "A";
In the second case foo( &a ) and foo( a ) are different!
Rigorously speaking, there is no "pointer to an array" in what you wrote
above. As I explained earlier, the difference in the first case is in the
types of the expressions, while the difference in the second case applies
to values as well as types.
--
Doug Harrison
Visual C++ MVP
.
- Follow-Ups:
- Re: Passing pointer to array of structs
- From: Vladimir Grigoriev
- Re: Passing pointer to array of structs
- References:
- Passing pointer to array of structs
- From: goodTweetieBird
- Re: Passing pointer to array of structs
- From: Victor Bazarov
- Re: Passing pointer to array of structs
- From: Vladimir Grigoriev
- Passing pointer to array of structs
- Prev by Date: Re: Passing pointer to array of structs
- Next by Date: Re: Passing pointer to array of structs
- Previous by thread: Re: Passing pointer to array of structs
- Next by thread: Re: Passing pointer to array of structs
- Index(es):
Relevant Pages
|