0

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.

Wolff
  • 1,051
  • 3
  • 18
  • 31
  • make the developer pass the character array as parameter and use sprintf – David Haim Dec 08 '15 at 14:27
  • And pay attention to compiler warnings, which should certainly have been given for that `return` line. – interjay Dec 08 '15 at 14:29
  • You can't figure out how C strings work by guessing, you actually have to read the chapter about string handling in your C book. And before that, the chapter about arrays and pointers. Stack Overflow is not a replacement for studies. – Lundin Dec 08 '15 at 14:30
  • Strings in C are `'\0'`terminated character arrays. Working with them is very different then it is in Java. I recommend you read a C book. – Magisch Dec 08 '15 at 14:40
  • This might be useful: http://stackoverflow.com/questions/3774417/sprintf-with-automatic-memory-allocation – Ian Abbott Dec 08 '15 at 14:40

2 Answers2

2

This isn't doing what you think:

return("%d:%d:%d:%d",days,hours,minutes,seconds);

This is actually a sequence of comma operators. The comma operator evaluates each expression in order and the whole expression has the value of the last expression. So this is the same as:

return seconds;

Which should be throwing a compiler warning.

You also can't do this:

char time[11] = timeConvert(61);

Since timeConvert returns a char *, while time is an array.

You probably want to pass your array to this function which will then populate it using sprintf.

void timeConvert(char time[], int secs){
    ...
    sprintf(time,"%d:%d:%d:%d",days,hours,minutes,seconds);
}

int main(){
    // make this long enough for the NULL terminator
    // plus some extra chars in case you pass in a larger value
    char time[15];
    timeConvert(time,61);
    printf(time);
    return 0;
}

Also note that time is larger. If days, hours, minutes, and seconds are all double digit numbers, the total length of the string is 11 characters plus 1 for the NULL terminator for a total of 12. Defining it as char time[11] doesn't give you enough space, and you end up writing past the end of the array, causing undefined behavior.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

If you want to return a string out of your function you have to use sprintf(). See the example at the following link. With this you can format an output and store it in a string.

C Reference sprintf()

Further to keep you application open for debugging purposes you can add a scanf() in the main or a while(1) as the last line before return 0 to prevent the application that it closes itself.

Frodo
  • 749
  • 11
  • 23