-4

I'm still new to C++ and I've got a question concerning arrays as parameters in functions/ constructors. I've realized that it is possible to declare an array in two ways (maybe there are more, which I haven't seen before):
1.

    void foo(int arr[]);

2.

    void foo(int arr);

In both cases, arr can be used in the following way:

   arr[0] = 1;

But what is the difference between those? Or is it just a matter of clarity?

Areopag
  • 43
  • 7
  • 2
    I've never heard of `[` and `]` being referred to as angular braces, people just call them "brackets" or "square brackets". Sometimes I hear `<` and `>` referred to as "angle brackets". – byxor Dec 29 '17 at 23:49
  • 2
    Those are *square brackets* not *angular braces*. :) – Galik Dec 29 '17 at 23:49
  • 5
    Perhaps you mean `(int *arr)` in your number 2? – Grantly Dec 29 '17 at 23:50
  • 3
    `{}` - *braces*, `<>` - *angular brackets*, `[]`- *square brackets* `()` - *parentheses*. – Galik Dec 29 '17 at 23:50
  • @Galik `{}` are also sometimes called _curly brackets_ – Killzone Kid Dec 29 '17 at 23:52
  • @Galik `{}` are often mentioned as curly brackets as well, but that's not the point of the question, is it? – kyriakosSt Dec 29 '17 at 23:53
  • You are right. I just mixed it up with the pointer to an array. Even though I knew that an array can be declared using a pointer, I thought there also was a way to declare an array like that. – Areopag Dec 29 '17 at 23:53
  • @KyrSt That's why its a comment and not an answer. – Galik Dec 29 '17 at 23:54
  • Sure, but it kind of got messy here, wasn't trying to be rude – kyriakosSt Dec 29 '17 at 23:55
  • 1
    Also, https://stackoverflow.com/questions/16748150/difference-between-int-array-and-int-array-in-a-function-parameter – AnT stands with Russia Dec 29 '17 at 23:56
  • In both cases, `arr` is a pointer. – Keith Thompson Dec 29 '17 at 23:59
  • @Aeropag - an array cannot be declared using a pointer, or vice versa. They are different things entirely. However, a pointer can be assigned to contain the address of the first element of an array, and array syntax can be used on that pointer to access elements of the array, That is a SYNTACTIC equivalence (array syntax can be used to operate on a pointer, and pointer syntax can be used to operate on an array). They are still VERY different things. – Peter Dec 30 '17 at 01:00
  • @Peter That's actually what I've meant, but I expressed it wrong. So, thank you. – Areopag Dec 30 '17 at 01:04

1 Answers1

1

The difference is one is a variable, the other an array.

void my_func(int array[]); // Function with array parameter.
void your_func(int variable); // Function with variable parameter.

The notation:

int v;
v[5] = 6;

should generate a compilation error or warning because the v variable is not an array nor a pointer.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154