Tuesday, 10 October 2017

Passing arrays by reference in C++

itemprop="text">












I
am trying to pass the arrays by reference. The problem is I am getting errors for
passing these arrays.





error C2664: 'InitializeArrays' : cannot convert parameter 1 from 'int [64]' to
'int
(&)[]'




Here is
the code:



void InitializeS(int
(&s)[], int (&BeforeDecimal1)[]);


int
main()
{
int BeforeDecimal[128],s[128];


InitializeS(s,BeforeDecimal);

return
0;
}

void InitializeS(int (&s)[], int
(&BeforeDecimal1)[])

{
for(int
i=0;i<128;i++)
{
s[i]=0;

BeforeDecimal1[i]=0;

}
}


What am
I doing wrong?



itemprop="text">
class="normal">Answer



In C++
there is unlike as in C no concept of compatible types and the type T[] is unrelated to
T[N]. You need to make the reference have a size and the size must equal the one of the
array you pass.



C++ also bans references to
arrays without bounds as function parameter types (perhaps for this
reason).


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...