-2

I wrote "lightweight" time library and I have struct and typedef like this:

struct tmt {
    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    uint8_t weekday;
    uint8_t is_dst; 
};

typedef struct tmt tm_t;

I have a function that returns tm_t:

tm_t rtc_get_current_time(void){
    tm_t tm;
    xSemaphoreTake(rtc_mutex, RTC_MUTEX_MAX_WAIT);
    tm = rtc.current_time;
    xSemaphoreGive(rtc_mutex);
    return tm;
}

And I want to use it like this:

tm_t s;
s = rtc_get_current_time();    // error is here

I'm getting this error:

incompatible types when assigning to type 'tm_t' {aka 'struct tmt'} from type 'int'


I have also tried to change function and variable like this:

struct tmt rtc_get_current_time(void){
    tm_t tm;
    xSemaphoreTake(rtc_mutex, RTC_MUTEX_MAX_WAIT);
    tm = rtc.current_time;
    xSemaphoreGive(rtc_mutex);
    return tm;
}

struct tmt tm = rtc_get_current_time();

What I am doing wrong?

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • 1
    @Kamil What is the type of rtc.current_time? – Vlad from Moscow Feb 25 '22 at 18:59
  • @VladfromMoscow it is `tm_t`, but the error is not at this line. It is at `s = rtc_get_current_time();`. Sorry for not pointing that. – Kamil Feb 25 '22 at 19:01
  • The root of your problems seems to be that you are using an outdated compiler or running it in "outdated mode". See [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Mar 02 '22 at 07:19

1 Answers1

5

The compiler is complaining that you are trying to assign an int to a tm_t, but since you implemented rtc_get_current_time() as returning a tm_t, this implies that the compiler thinks rtc_get_current_time() is returning an int instead of a tm_t.

That can happen if rtc_get_current_time() has not been declared yet in the context where it is being called. The C language allows a function to be called without a prior declaration, but then you end up with the compiler assuming a default declaration for the function, and that default uses int as the return type. You don't want that in this case.

You need to declare rtc_get_current_time() before you call it.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Oh dear... I missed include. I didn't expected such compiler behavior. I have to read some books. – Kamil Feb 25 '22 at 19:04
  • 2
    Re “The C language allows a function to be called without a prior declaration…“: Implicit function declarations were removed from the C standard in the 2011 version. An undeclared identifier is a violation of the language syntax (C 2018 note 94), so a conforming declaration must diagnose it. – Eric Postpischil Feb 25 '22 at 19:18