Check if element exists in array

Does anyone know what the value is of an element that doesn't exist or how I can check if it exists or not? I've tried numerous conditional statements, the following being what I thought would be most likely to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DWORD mouseEvents[] = {
						MOUSEEVENTF_LEFTDOWN, 
						MOUSEEVENTF_LEFTUP,
};

int mouseReq = gLua->GetInteger(1);

DWORD mouseAct = mouseEvents[mouseReq];

if (mouseAct != NULL)
{
	mouse_event(mouseEvents[mouseReq], NULL, NULL, NULL, NULL);
}
else
{
	gLua->Error("Invalid index number specified.");
};
You are trying to tell whether a c-style array index is in range? Or whether it has valid data? Or both?

You have to check whether the index mouseReq is in range before doing this: DWORD mouseAct = mouseEvents[mouseReq];

Alternatively, you can use a vector and use the at() member function which will throw an out_of_range exception.

If it's possible to have invalid data within a valid range, then you need to set them to an unused value (maybe NULL).
Topic archived. No new replies allowed.