I want to know how to use a typedef struct outside the file where it is located. I mean, I have this:
In a File called “rtc_i2c.c” I have ————————————
#include "rtc_i2c.h"
struct time_date_str
{
char year;
char month;
char date;
char day;
char hour;
char minute;
char second;
};
int RTCGetTime(TimeDate *timdatstrptr)
{
timdatstrptr -> second = 0x12;
return 0;
}
Then in a file called “rtc_i2c.h” I have ——————————-
#ifndef RTC_I2C_H
#define RTC_I2C_H
typedef struct time_date_str TimeDate;
#endif
And in “main.c” I have ————————————————-
#include "rtc_i2c.h"
TimeBase *TimeDateData;
void main(void)
{
char DateTimeASCII[20];
RTCGetTime(TimeDateData);
DateTimeASCII[0] = ????????
}
Then when I step in to my code I enter in to RTCGetTime and in a watch window I can see the “timdatstrptr -> second = 12” assignment executing correctly. But the problem is outside. The compiler generates an error if I put “TimeDateData –> second” in the space with ????????. So I don’t know how am I supposed to access the contents of my globally defined array through a pointer.
If I understood, the pointer declared as “TimeBase *TimeDateData” is pointing to my array and is accessed with the “–>” because is a pointer. Am I wrong? how does this work?
Can anyone help me please? Thanks!!
The compiler doesn’t know the details of the
time_date_str. You have 2 choices:struct time_date_strdeclaration into the header filertc_i2c.cIt all boils down to the question: should outside entities know details about this structure ?
There are other problems as well, including the fact that you’re passing an uninitialized pointer to
RTCGetTime.