Good idea gone bad

Greetings and Salutations! Here are the forces involved:

int mosPos[2];

SDL_GetMouseState(&mosPos[0], &mosPos[1]);

int getHexFromXYPos(int[2]); // returns -1 if it fails to find a match

In the scope of getHexFromXYPos() there is only one value (specifically mosPos[0]). Why the one value?
I don't know SDL, but it's not feeling right on

int getHexFromXYPos(int[2]);

It's a declaration, and you're not actually calling it.
Last edited on
The x and y coordinates are passed to the function using an array. The first array element is the x-coordinate and the second array element is the y-coordinate.
Last edited on
Note that debuggers may not be able to know that the array pointed to by the parameter is of size 2, and may show only one element.
@liuyang you are right that is it's declaration here is it's call from main():
int ref = daGrid.getHexFromXYPos(tools.mosPos);

@Peter87 absolutely correct

@helios I do know that it's impossible to pass an entire poly dimensional array to a function (granted this is based on what I learned with VS2010)

however for a mono dimensional array any of the three following ways should work:
as f(int *a), I understand by default arrays are passed by reference
as f(int a[x]) explicitly telling a function expect an array of size x
int getHexFromXYPos(int[2]); I'm fairly sure I did...
as f(int a[]) explicitly telling a function to expect an array of unknown size.
Please let me know if I'm wrong, wont learn otherwise

The work around isn't difficult so I'm not heartbroken if that has to be the case (I verify what helios says in that only the first element is getting passed)

if it helps here is the function in question
int::Grid::getHexFromXYPos(int mosPos[2])
{
for (int i = 0; i < hexGrid.size(); i++)
{
if (hexGrid.at(i)->xPos >= mosPos[0] + 40 && hexGrid.at(i)->xPos <= mosPos[0] + 40
&& hexGrid.at(i)->yPos <= mosPos[1] + 60 && hexGrid.at(i)->xPos >= mosPos[1] + 60)
{
return hexGrid.at(i)->vectPos;
}
}
return -1; // on the event no such hex exists... this should never happen
}

and I forgot to say thank you for your time in my original post, so thank you!

All of these are handled exactly the same.

1
2
3
f(int *a)
f(int a[x])
f(int a[])

The compiler treats a as a pointer and doesn't care about the size x. When you pass an array to this function what is actually being passed is a pointer to the first element in the array.
So:
int arr[3] {1,2,3};

for(int i = 0; i <someNum; i++)
{
someFunction(arr[i]);
}
The pointer to arr[i] is what is being passed then?
No. It only applies to arrays.
I do know that it's impossible to pass an entire poly dimensional array to a function (granted this is based on what I learned with VS2010)
Why do you think so? It is sure possible.

as f(int a[x]) explicitly telling a function expect an array of size x
No:

f(int (&a)[x]); // This tells the compiler what array to expect/pass

Note that x needs to be constant.
Topic archived. No new replies allowed.