I'm trying to return an array from a function and to do that a have a pointer in the function but when i try to return the array it returns only the first element! :S
This is my code:
int* getDate() {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
int i;
static int date[7];//i save in each position the year, month, day, h, m and s
date[0] = tm.tm_year + 1900;
date[1] = tm.tm_mon + 1;
date[2] = tm.tm_mday;
date[3] = tm.tm_hour;
date[4] = tm.tm_min;
date[5] = tm.tm_sec;
return date;
}
int main(){
int *p;
int i;
p = getDate();
for (i = 0; i<6; i++){
printf("%d/", *p);
}
return 0;
}
Desired output: 2014/12/24/3/44/12 (year/month/day/hour/minute/seconds)
Current output: 2014/2014/2014/2014/2014/2014
p[i]or*p++? As written, you print the first element ofpsix times.