-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.

5
  • 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]. Commented Jul 25, 2022 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. Commented Jul 25, 2022 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. Commented Jul 25, 2022 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. Commented Jul 25, 2022 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. Commented Jul 25, 2022 at 16:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. And what is considered the "best practice", considering the two notations are equivalent?
@RiccardoPerego The pointer version, it's honest about what x is.
@RiccardoPerego I'd argue that the best practice isn't to pass pointers at all, but to use std::vector or std::array instead.
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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.