Re: Passing pointer to array of structs



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
.



Relevant Pages

  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: The question regarding type of pointers
    ... int day_of_year ... According to my understanding daytab is pointing to the whole daytab ... array i.e it is equivalent to p3. ... daytab is converted to a pointer to the first ...
    (comp.lang.c)
  • Re: How would you design Cs replacement?
    ... I would get rid of void. ... a member called size of type int. ... size of the array pointed at by v->dynamic is given by int size. ... If you pass an unsized pointer to a sized parameter, ...
    (comp.lang.c)
  • Re: Why is the pointer passed into the function still NULL?
    ... you must pass a *pointer* to the thing you want to modify. ... void foo (int x) ... The expression x in foo and the expression a in bar refer to ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)