Initializer fails to determine size of 'str'

I'm having trouble with this code here:

int hours;
    int minutes;
    int seconds;
    int cnt = 0;
    char str[] = rtc.getTimeStr();
    int init_size = strlen(str);
    char delim[] = ":";

    char *ptr = strtok(str, delim);
    
    while(ptr != NULL)
    {
      cnt += 1;
      if(cnt == 1){
        hours = ptr;
      }
      if(cnt == 2){
        minutes = ptr;
      }
      if(cnt == 3){
        seconds = ptr;
      }
    }

Upon verifying it gives me this error: Initializer fails to determine size of 'str'.
What could be causing this? Is it because I don't have the RTC module hooked up?

What could be causing this?

Some function, in some other file, is going to return a NULL terminated collection of characters. The compiler has NO idea how many characters will be returned, so it can't size the array for you.

You must determine the longest string that the function will return, and size the array yourself.

Notice that it makes no sense to call that function at compile time.

You need to post the complete program. And the actual error message.

...R

PaulS:
Some function, in some other file, is going to return a NULL terminated collection of characters. The compiler has NO idea how many characters will be returned, so it can't size the array for you.

You must determine the longest string that the function will return, and size the array yourself.

Notice that it makes no sense to call that function at compile time.

Alright, now I did this:
char str[8] = rtc.getTimeStr();
Now it gives me this error: array must be initialized with a brace-enclosed initializer

Have you, perhaps, been programming in Python, Ruby, Java, or another language that uses a garbage collector for memory management?

Yep, python and java

While the run-time libraries for C and C++ provide the means for managing memory that are not too different from what is available with Python and Java, neither language provides anything close to what you can do in Python and Java. In addition, you are now working with a memory constrained device. That means memory management is now your responsibility.

For empty brackets to work...

    char delim[] = ":";

...a constant initializer has to be provided so the compiler knows what size to make the array. In that example, delim is given two bytes of storage: one for the colon and one for the terminating null.

In this example...

    char str[] = rtc.getTimeStr();

...rtc.getTimeStr cannot possibly provide a constant so the compiler has no way to determine what size to make str.

Note: delim is a waste of resources. I suggest removing it...

    char *ptr = strtok(str, ":");

It still makes no sense to be calling rtc.getTimeStr() at compile time. The ONLY time that the value is meaningful is at run time. So, separate the declaration of the array from the initialization of the array, and your problems will go away.

You want to make certain that rtc.getTimeStr() returns an array of chars. It might (high probably, really) return a pointer.

Okay, thanks. I ended up using char* str instead of char str[]