• 0

[C++] Passing dynamic array into a function


Question

How would I pass a dynamic array into a function by reference? By the way this is my array:

int *element = new int[N]

Thanks!

Edited by saiz66
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Arrays are passed by reference by default as the array variable is actually a pointer to the first element of the array. Your prototype for your function would be smiliar to...

void MyFunc( int* array, const int lengthOfArray );

If you don't want side effects( changes in your array ), you'd need to specify the array as const in the parameters of the function. Nothing changes in the way the function is called. It would be a compile error if you tried to modify the array in MyFunc.

void MyFunc( const int* array, const int lengthOfArray );

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.