Results 1 to 8 of 8

Thread: passing array[5][5] to a function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89

    passing array[5][5] to a function

    How can I pass something like

    int array[5][5];

    to a function

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    what would I do if I didn't know the size of the array

    Thanks

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Question 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!
    Baaaaaaaaah

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width