Click here to Skip to main content
15,877,280 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am confused how I can declare and call the function with call by reference array as argument. I wrote a code and I got an error for declaration of the function about call by reference array. How can I solve that.

This is a declaration of the function:

C++
void lenEqualize(int &updDisStr2[], int shortLenStr[], int diffLen, int updLen);
// updDisStr2: is an array which should be called by reference
// shortLenStr: call by value array


call the function:
C++
lenEqualize(updDisStr2[], equiDisStr2[], diffLen, updLen)
Posted
Comments
Afzaal Ahmad Zeeshan 5-Aug-15 4:39am    
Why are you even appending the indexers? Remove them.
amin.j 5-Aug-15 4:44am    
I removed but it is not solved.
[no name] 5-Aug-15 4:50am    
This is more complex than your example shows. We need all your code. Why do you wish to pass explicitly by reference? Only arrays of known size can be passed by reference in this manner. Is your array of known size? eg int marray[10];
amin.j 5-Aug-15 4:52am    
You got that. It needs size. But the size is not specific at the start of program. Is it possible to have a vector with call by reference?

Quote:
void lenEqualize(int &updDisStr2[], int shortLenStr[], int diffLen, int updLen);

First argument is an array of references to int values ( the [] operator binds tightly than the & one, see "C++ operator precedence"[^]). Anyway I don't get your point why should you pass an array by reference?
 
Share this answer
 
Comments
amin.j 5-Aug-15 4:49am    
I want to change the values of that array in the function. And don't return sth.
CPallini 5-Aug-15 5:06am    
You already can: there's no need to pass it by reference. C/C++ pass the array 'name' that is a pointer to the first item by value that prevents the modification of the pointer value itself NOT of the pointed elements (the array items).

try
void fun( int a[])
{
a[1] = 42;
}
int main()
{
int b[] = {1,2,3};
fun(b);
std::cout << b[1] << std::endl;
}
You seem to be confused about how to reference arrays. An array declared as int myints[5] can be referenced just by its name alone, no prefix ampersands and no suffix brackets. I'm not sure what you mean by call by value array.
So your code should be something like:


C++
void lenEqualize(int* updDisStr2, int* shortLenStr, int diffLen, int updLen)
{
    // updDisStr2: is a pointer to an array
    // shortLenStr: is also a pointer to an array
}
//call the function:
int updDisStr2[10];
int shortLenStr[10];
int diffLen;
int updLen;
lenEqualize(updDisStr2, equiDisStr2, diffLen, updLen)</p>

Note that the names declared in the function parameters are local to that function and do not need to match the originals.
 
Share this answer
 
v2
Comments
amin.j 5-Aug-15 5:12am    
I corrected the code by your comments, the error is solved. Now, how can I reach the element of updDisStr2? I used this for the first element of this array:

cout << *updDisStr2[0] ;


but it sends error.
Richard MacCutchan 5-Aug-15 5:18am    
Yes because you are trying to dereference something that is not a pointer. You should use either:
cout << *updDisStr2; // the element that updDisStr2 is pointing to
or
cout << updDisStr2[0]; // the element at offset 0 of updDisStr2
Which, in this case are both the same. You can iterate through the elements by using the name as a pointer

for (int i = 0; i < MAXVALUE; ++i)
{
cout << *updDisStr2++; // increment the pointer after writing the current value
}

or use explicit offset values.

for (int i = 0; i < MAXVALUE; ++i)
{
cout << updDisStr2[i]; // i is the current offset
}
amin.j 5-Aug-15 5:29am    
This is part of my code that gives error: when I changed that to
cout << updDisStr2[0];
now it say that updDisStr2 is not declared, but I declared it in the if. I am really confused?



if (lenStr1 > lenStr2){
updLen = lenStr1;
int updDisStr2[updLen];
lenEqualize(updDisStr2, equiDisStr2, diffLen, updLen);
}
else if (lenStr1 < lenStr2){
updLen = lenStr2;
int updDisStr1[updLen];
lenEqualize(updDisStr1, equiDisStr1, diffLen, updLen);
}
Richard MacCutchan 5-Aug-15 7:21am    
You need to show us the code for the lenEqualize function. Try and collect all the relevant information together so we can see exactly what you are doing.
Richard MacCutchan 5-Aug-15 7:53am    
As I suggested in your other question. You really need to get hold of a good C++ book and study the basics. The difference between an element and an array, and the use of pointers versus indexers is something that you really need to understand fully. Also you need to understand scope and lifetime of variables. You cannot safely create a local array inside a function and return it to the caller by address.
You need to uses template and add missing parenthesis if you want to pass reference and have the size and the caller must have array with known size.

For flexibility, you should use std::vector instead.

By the way, you cannot use the reference syntax if you want to create a new array or chane its size inside the function.

Although it can be done with pointers using something like Solution 2, I would recommand to use STL containers like vector instead. It is much more maintainable as the size and the data are kept together and would always match.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900