3

Possible Duplicate:
Confusion over C++ pointer and reference topic

Suppose I am passing

int arr[10];

as a parameter to a function.

Are all of these valid function prototypes? How do they differ in terms of arguments and why?

This is what I know so far (not sure if correct or not)

1. void foo(int &arr);      //a reference to an array, which preserve the size attribute?
2. void foo(int &arr[]);    //the same (is it)?
3. void foo(int (&arr)[]);  //I don't know
4. void foo(int &arr[10]);  //is it the same as 2?
5. void foo(int (&arr)[10]);//no idea

6. void foo(int *arr);      //a decayed pointer of the array, pointing to the first element of the array, losing the size attribute?

7. void foo(int *arr[]);    //the same (is it?)
8. void foo(int (*arr)[]);  //a pointer to an array, preserve the size
9. void foo(int *arr[10]);  //the same as 7 (is it?)
10. void foo(int (*arr)[10]);//is the same as 8 (is it?)

11. void foo(int arr[]);    //is the same as 6 (is it?)
12. void foo(int arr[10]);  // is the same as 6 (is it?)

(I know this will need a lengthy explanation, sorry, I'm totally confused...)

Community
  • 1
  • 1
Chin
  • 19,717
  • 37
  • 107
  • 164

2 Answers2

8

The first important piece of information is that parameters whose type is a (bounded or unbounded) array of T are transformed to pointers to T. I.e. both int arr[] and int arr[10] are transformed to int * arr. Note that the transformation is only performed on top-level arrays, i.e. it doesn't occur in int (*arr)[10], which is a pointer to an array of int.

Furthermore, things to the right of the identifier bind more closely than things to the left, i.e. int *arr[10] is an array, whereas int (*arr)[10] is a pointer.

Lastly, arrays of and pointers to references are invalid, as are pointers and references to unbounded arrays.

1. void foo(int &arr);        // can't pass, reference to int
2. void foo(int &arr[]);      // invalid, pointer to reference to int
3. void foo(int (&arr)[]);    // invalid, reference to unbounded array of int
4. void foo(int &arr[10]);    // invalid, pointer to reference to int
5. void foo(int (&arr)[10]);  // can pass, reference to an array of int

6. void foo(int *arr);        // can pass, pointer to int
7. void foo(int *arr[]);      // can't pass, pointer to pointer to int
8. void foo(int (*arr)[]);    // invalid, pointer to an unbounded array of int.
9. void foo(int *arr[10]);    // can't pass, pointer to pointer to int
10. void foo(int (*arr)[10]); // can't pass, pointer to array of int

11. void foo(int arr[]);      // can pass, pointer to int
12. void foo(int arr[10]);    // can pass, same as above

Using arr as an argument to foo will cause it to decay to a pointer to its first element -- the value passed to foo will be of type int *. Note that you can pass &arr to number 10, in which case a value of type int (*)[10] would be passed and no decay would occur.

avakar
  • 32,009
  • 9
  • 68
  • 103
  • 2
    There are a lot of mistakes here. For example, #3 and #8 are invalid, and #10 is not the same as #9. Also, you didn't answer the actual question - to which functions can `int arr[10]` be passed. – interjay Oct 17 '12 at 17:31
  • 1
    *#10* is a pointer to an array of 10 integers, not just an `int **` – Praetorian Oct 17 '12 at 17:32
  • whoops my bad..., @MooingDuck, thanks for the edit! – avakar Oct 17 '12 at 17:40
  • Refs: 8.3.4/1+2 (meaning of array decls), 8.3.5/5+8 (adjustment of "array of" to "ptr to" in function decls) – dyp Oct 17 '12 at 17:45
  • Coming across this when I'm currently trying to solve my own problem with passing arrays into functions, it would be handy to have corresponding examples oh how to call foo from the valid examples. Possibly even examples ways to access the data from foo – ChiggenWingz Apr 15 '15 at 23:00
0

The difficult part is to consider that array don't pass by value, but decay into pointers.

Some of your declaration are syntax errors, some other are not (but are also not what you probably think)

In your case, teh only one that make sense are 6, 11 and 12.

2, 3 ,4 and 8 have self explanatory error messages. (if you don't understand them is most likely because you read the declaration with a wrong operator priority)

t1.cpp:2:19: error: declaration of 'arr' as array of references
t1.cpp:3:22: error: parameter 'arr' includes reference to array of unknown bound 'int []'
t1.cpp:4:21: error: declaration of 'arr' as array of references
t1.cpp:8:22: error: parameter 'arr' includes pointer to array of unknown bound 'int []'

The others are somehow redundant (reference-to-array or -pointer-, they will behave the same inside the function) or simply wrong because declaring something different as you intend (like 7,9,10: they represent a "double indirection", while a plain array has a single one, and 1. that doesn't represent any indirection: it just aliases a single int)

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63