struct tm * data_emprestimo
declares a pointer to a struct, so to return the struct itself as a value, you need to dereference the pointer, return *data_emprestimo;
.
题
I have the following snippet of code:
time_t data1 = time(0)+86400;
struct tm * data_emprestimo = localtime( & data1 );
cout << "Hoje: " << data_emprestimo->tm_mday << '/' << (data_emprestimo->tm_mon + 1) << '/' << (data_emprestimo->tm_year + 1900) << endl;
It works well.
But I wonder what kind I should return in a function to get what cout echos and put on a variable: struct tm? Just tm? Array? String?
I've tried something like this:
struct tm retornaData(int segundosAdd);
...
...
struct tm retornaData(int segundosAdd){
return data_emprestimo;
}
but it did not work.
And I already googled that a lot!
解决方案
struct tm * data_emprestimo
declares a pointer to a struct, so to return the struct itself as a value, you need to dereference the pointer, return *data_emprestimo;
.
其他提示
It seems to me that you wish to write a function that returns a string representation of a date.
Within c++ std::string would be the natural return type for string data. A more C-style approach might suggest you pass in a char * as a the buffer to which you wish the date printed - in general std::string is seen to be a cleaner and more robust approach.