-1

What is the difference between taking as a function argument an int pointer or an int array in C++?

void arrayFunction1(int * x) {
  for(int i = 0; i < 10; i++) {
    cout << x[i] << endl;
  }
}

void arrayFunction2(int x[]) {
  for(int i = 0; i < 10; i++) {
    cout << x[i] << endl;
  }
}

int main() {
  int dstdata[10];
  
  arrayFunction1(dstdata);
  arrayFunction2(dstdata);
  
  return 0;
}

Both results look the same to me.

Riccardo Perego
  • 383
  • 1
  • 11
  • 5
    `int[]` as a parameter type does not mean "array of `int`", it means "pointer to `int`". Your prototypes are equivalent, and both function arguments are equivalent to passing `&dstdata[0]`. – molbdnilo Jul 25 '22 at 15:51
  • Yeah, they're exactly the same, which is an endless source of confusion for people thinking they'd be different and then wondering why `sizeof(x)` gives unexpected results. – Etienne de Martel Jul 25 '22 at 15:52
  • @EtiennedeMartel Exactly. Why does sizeof(dstdata) return 40 and sizeof(x) return 8? I've learnt that to calculate the length of an array I I can calculate sizeof(array) / sizeof(type). This won't work in the function call. – Riccardo Perego Jul 25 '22 at 15:58
  • 1
    An array in C++ is `std::array`. You should start with STL and go to pointers, C-arrays and other advanced stuff after you've learned the basics. A C++ array can be copied and passed to functions. Either learn C or C++, not both at the same time. That's confusing and leads to very bad practices. – jabaa Jul 25 '22 at 15:59
  • It won't work because when you declare an array _as a function parameter_ (and only there), you're actually declaring a pointer. It's a weird quirk of the language that C++ inherited from C. You really should be using `std::array` instead of C arrays. – Etienne de Martel Jul 25 '22 at 16:01

1 Answers1

2

There are completely identical. In a function parameter int x[] is just another way of writing int* x. Even int x[10] is the same (the 10 is completely ignored).

Now why Kernighan and Ritchie (the inventors of C) thought this piece of deception was a good idea is another question. I guess they weren't thinking of all the people who have to learn the syntax.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks for the answer. And what is considered the "best practice", considering the two notations are equivalent? – Riccardo Perego Jul 25 '22 at 15:52
  • 2
    @RiccardoPerego The pointer version, it's honest about what `x` is. – john Jul 25 '22 at 15:53
  • 2
    @RiccardoPerego I'd argue that the best practice isn't to pass pointers at all, but to use `std::vector` or `std::array` instead. – Some programmer dude Jul 25 '22 at 15:54
  • Or you could follow the standard library's approach and instead of passing a container directly, you pass a starting iterator and a sentinel value indicating the end of the range. – Nathan Pierson Jul 25 '22 at 15:54
  • @Someprogrammerdude Yes, I just started learning C++ so I haven't got there yet. Was just wondering whether the two were equivalent. – Riccardo Perego Jul 25 '22 at 15:55
  • @RiccardoPerego If you really want `dstdata` to be an `int[10]`, pass it to functions wrapped in a `std::span`. – paolo Jul 25 '22 at 15:55
  • 2
    @RiccardoPerego unfortunately most tutorials and courses teach it the wrong way around. They start with stuff that is rarely used in real coding and keep the idomatic stuff for the later chapters. `std::array` and `std::vector` are the beginner-friendly counter parts for c-arrays. – 463035818_is_not_an_ai Jul 25 '22 at 16:11