Firstly, I'm new to C, and have only a very basic understanding of it. I haven't got my head round the whole idea of pointers and memory management yet, so I'm still approaching it from a Java mindset - so any 'pointers' in the right direction would be appreciated. (Excuse the pun)
I have a function that converts an integer of seconds into a string in the format "days:hours:minutes:seconds".
I'm trying to initialize a string by calling that function on an integer, but
const char * timeConvert(int secs){
...
return("%d:%d:%d:%d",days,hours,minutes,seconds);
}
int main(){
char time[11] = timeConvert(61);
printf(time);
return 0;
}
"..." is code that does the conversions into days/hours/mins/secs and is not relevant to the issue.
With this code I get an error on the line "char time[11] = timeConvert(61);" saying "error: invalid initializer"
If I remove that line and replace main with this...
int main()
{
printf(timeConvert(61));
return 0;
}
...The program appears to compile but crashes with a "A problem causes the program to stop working correctly" error.
Can someone tell me why these approaches do not work, and how to work around this?
I essentially want to input an integer to a function that gives me back a string in the format "days:hours:minutes:seconds" which I can then print to the console.