Click to See Complete Forum and Search --> : Problems with passing arrays to a function


Shahzad Alam
April 14th, 1999, 08:23 AM
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!

yash
April 14th, 1999, 09:21 AM
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.

Franky Braem
April 14th, 1999, 09:33 AM
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.