|
-
Apr 28th, 2001, 09:07 AM
#1
Thread Starter
Lively Member
passing array[5][5] to a function
How can I pass something like
int array[5][5];
to a function
Thanks
-
Apr 28th, 2001, 01:28 PM
#2
Frenzied Member
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
}
that should do it
-
Apr 29th, 2001, 10:26 AM
#3
Thread Starter
Lively Member
what would I do if I didn't know the size of the array
Thanks
-
Apr 29th, 2001, 10:35 AM
#4
Monday Morning Lunatic
Just pass a pointer to the first element:
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
}
However, you need to be careful you don't go out-of-bounds on the passed array.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 29th, 2001, 01:10 PM
#5
Frenzied Member
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?
Harry.
"From one thing, know ten thousand things."
-
Apr 29th, 2001, 01:13 PM
#6
Monday Morning Lunatic
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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 4th, 2001, 06:51 PM
#7
PowerPoster
Does not execute the code!!
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!
-
May 5th, 2001, 06:39 AM
#8
Monday Morning Lunatic
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|