Referece in a function perimeter understanding

void ChargeTrack(tracks track[],GWindow &Gwin)
ChargeTrack(&track[0],Gwin);

tracks is a struct and track is a array in the main block of code

I am trying to create a referece in a function perimeter and my friend gave me this code and it works so i just put what needed to be said but when i create my own type it says:

When i try to create my own it gives me a cannot convert parameter 1 from 'int *' to 'int' message

void here(int x);
int main()
{
GWindow Gwin;
int x = 10;
here(&x);
return 0;
}
void here(int x)
{
x+=10;
}

Wondering what is the difference and why i get that error message
Last edited on
&x is not a reference. it is the address of x (it's position in memory) and it's type is int*. your function takes an int.

solution 1:
1
2
3
4
5
6
7
void here(int& x){ x += 10; }

int main(){
   int a = 10;
   here(a);
   return 0;
}


solution 2:
1
2
3
4
5
6
7
void here(int* x){ *x += 10; }

int main(){
   int a = 10;
   here(&a);
   return 0;
}
Why does the array get copied then? in the function it has all the numbers what i entered in the main and when the void closes it has the numbers i entered in the function in the main
in c++ passing an array to a function means passing it's pointer. "solution 2" would work fine if you replaced "int* x" with "int x[]".
Topic archived. No new replies allowed.