DDC DDC - 1 year ago 82
C++ Question

Why can you pass an array as a pointer?

I was reading http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm and the first method it recommends in passing an array to a function is to pass it as a pointer:

void myFunction(int *myArray) {

Wouldn't this not work, because it's impossible to determine the length of

Answer Source

The usual convention is to pass a separate parameter for the array length.

void myFunction(int* myArray, int length) {
   for (int index = 0; index < length; ++index) {
      // ... do something with each array item ...

void caller() {
    int array[10];
    // ... put some useful values in the array ...
    myFunction(array, sizeof(array) / sizeof(array[0]));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download