I want to return an array of my custom structure object timeInDay
with as few lines as possible, in order to return a day in my time schedule.
struct timeInDay[] getRingTimesForWeekDay(int day) {
switch (day) {
case MONDAY: return { {7, 50} };
case TUESDAY: return { {7, 50} };
case WEDNESDAY: return { {7, 20} };
case THURSDAY: return { {7, 50} };
case FRIDAY: return { {7, 50} };
case SATURDAY: return { {7, 50} };
case SUNDAY: return { {7, 50} };
}
}
struct timeInDay {
unsigned short hour;
unsigned short minute;
};
Right now it produces an error with the return value of the method:
error: decomposition declaration cannot be declared with type 'timeInDay'
struct timeInDay[] getRingTimesForWeekDay(int day) {
Would be deeply appreciated if anybody could write down their way of doing it with as few lines as possible.
No line is required to return a c-array from a function, because you cannot return a c-array from a function.
You could dynamically allocate it and return a pointer to first element and size, but you better stay away from that in favor of std::array
:
#include <array>
struct timeInDay {
unsigned short hour;
unsigned short minute;
};
std::array<timeInDay,1> getRingTimesForWeekDay(int day) {
switch (day) {
case 1: return { {7, 50} };
default: return { {7, 50} };
}
}
However, as others have mentioned already, the function does only return a single element, so it isnt clear why you want an array in the first place.
PS: If the size of the array is dynamic, use std::vector