Hi,
Just out of curiosity, how do I pass an array as an argument by value instead of by reference?
Is the only way to send the pointer of the array and then copy it manually?
Thanks in advance.
Hi,
Just out of curiosity, how do I pass an array as an argument by value instead of by reference?
Is the only way to send the pointer of the array and then copy it manually?
Thanks in advance.
You can't, unless you pass each element of the array separately.
zen
Pass a pointer to the first element of the array, and then increment the pointer with out doing any dereferencing, increment it.
Sean Mackrory
[email protected]
Declaration
int *array=new int[20];
Prototype
void UseArray(int *passedarray);
Calling
UseArray(array);
Both of the above two examples will pass the array by reference, not by value (any modification to the array in the function will be made to the original). Whenever an address of a variable is passed into a function it is passed by reference, and to pass an array you need to pass it's address.
zen
int array[10];
void UseArray(int &array)
{
int *ptr=&Array;
for (int i=0;i<endofarray;i++,ptr++)
{
printf("%d\n",*ptr);
}
}
UseArray(*array);
Don't use this way very often.
put the array in a struct and pass the struct by value.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Put the keyword "const" in front of the array ''
-----------------------------------------------
everready
To code, or not to code, that is the question.
Well the answer is 'TO CODE' of cause