How can I pass something like
int array[5][5];
to a function
Thanks
Printable View
How can I pass something like
int array[5][5];
to a function
Thanks
that should do it :)Code:int myArray[5][5];
int MyFunction(int Array[5][5]);
int main() {
MyFunction(myArray);
return 0;
}
in MyFunction(int Array[5][5]) {
//code here
}
what would I do if I didn't know the size of the array
Thanks
Just pass a pointer to the first element:
However, you need to be careful you don't go out-of-bounds on the passed array.Code:int myArray[5][5];
int MyFunction(int *piArray);
int main() {
MyFunction((float*)myArray); // The cast is there to make sure
return 0; // that it's interpreted correctly
// otherwise you'd need the following
MyFunction(&(myArray[0][0]));// <<< code :(
}
in MyFunction(int *piArray) {
//code here
}
You won't be able to access it as a 2-dimensional array in the function, though, because it's just a pointer and has lost the type information that the compiler has to tell it how big one of the array dimensions is. You probably ought to pass the dimensions as parameters (or at least one dimension) so that you can use pointer arithmetic to access it with the same array indices as you would use usually.
I'm not sure if that's confusing, I'm not sure I can explain it very well.
Why the cast to float* and not int*, Parksie?
Bugger. Sorry, copied it straight from my OpenGL program to pass matrices around so it was float* :(
Everyone, Harry's correct, it should be int* :)
I have written the following code to create a simple window. But the problem is that whenever I click execute button. It does not execute(run) it. It does nothing.
Please Note:
I am using Visual C++ 6 Enterprise, and every other program is executed except this one. I also copied and pasted the code from www.winpro.org to create a simple window and it works fine. Please find any error if you can.
#include <windows.h>
static char myclass[] = "t";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return WndProc(hwnd, msg, wparam, lparam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc;
MSG message;
HWND hwnd;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszMenuName = myclass;
wc.lpszMenuName = NULL;
wc.style = NULL;
if(!RegisterClassEx(&wc))
{
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
myclass,
"This is win",
WS_OVERLAPPEDWINDOW,
0,
0,
100,
100,
NULL,
NULL,
hInstance,
NULL);
if(hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&message, NULL, 0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
Thanks!:confused:
Step through it using the debugger (set a few breakpoints on important lines using F9), by pressing F5 on its own. That should help you a bit :)