The title may not be clear so I'll give an example.
I am trying to make a system of "data streams" in C.
Type STREAM
:
typedef struct {
void (*tx) (uint8_t b);
uint8_t (*rx) (void);
} STREAM;
I have a file uart.h
with uart.c
which should provide a STREAM
for UART.
I decided it'll be best to expose it as a pointer, so it can be passed to functions without using ampersand.
This is the kind of functions I want to use it with (example):
/** Send signed int */
void put_i16(const STREAM *p, const int16_t num);
Here's my UART files:
uart.h
extern STREAM* uart;
uart.c
// Shared stream instance
static STREAM _uart_singleton;
STREAM* uart;
void uart_init(uint16_t ubrr) {
// uart init code here
// Create the stream
_uart_singleton.tx = &uart_tx; // function pointers
_uart_singleton.rx = &uart_rx;
uart = &_uart_singleton; // expose a pointer to it
}
I'm not sure about this. It works, but is it the right way to do it? Should I just use Malloc instead?
Why I ask this, it's a library code and I want it to be as clean and "correct" as possible