CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Location
    Pakistan
    Posts
    19

    Problems with passing arrays to a function

    I am passing any array of COleDateTime to a fuction to perform some operatios. But the array is not passed except for the first entry and even gives an exception when the function return value.
    My code is:

    // Before I call the function, initialize the time values
    COleDateTime[5] time;
    time[1].SetTime(6,0,0);
    time[2].SetTime(10,0,0);
    time[3].SetTime(14.0,0);
    time[4].SetTime(20,0,0);

    // calling function now to pass the time object
    int value;
    value = MyFunction(time);
    *************************************
    // Implementation of MyFuction
    int MyFunction(COleDateTime *time)
    {
    CString str[5]; // just to convert time to CString and display
    str[1]=time[1].Format("%H:%M:%S");
    str[2]=time[2].Format("%H:%M:%S");
    str[3]=time[3].Format("%H:%M:%S");
    str[4]=time[4].Format("%H:%M:%S");

    AfxMessageBox(str[1]);
    AfxMessageBox(str[2]);
    AfxMessageBox(str[3]);
    AfxMessageBox(str[4]);

    return 0; // just a test here!
    }

    This function does not display the 4 times but just the first one. Please help! How can I pass the array to the function correctly. This function generates an exception when it returns the value. Please help!


  2. #2
    Join Date
    May 1999
    Location
    Houston - TX - US
    Posts
    29

    Re: Problems with passing arrays to a function

    Hi!
    declare the array of time of type COleDateTime as

    COleDateTime time[5];//COleDateTime[5] time; is wrong n U have done it this way?

    everything should be fine then !

    Good Luck.
    Yash.



  3. #3
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Problems with passing arrays to a function

    Try this in your definition :

    int MyFunction(COleDateTime time[], int nCount)



    You have to pass a counter because you can't determine the size of your array.
    Why don't you use element 0 ? An array in C++ is zero-based.

    You can also do the following :

    int MyFunction(COleDateTime *time, int nCounter)
    {
    ...
    }




    call the function as follows :
    MyFunction(&time[0], 5);


    you pass the address of the first element.


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

Featured