As you can see, I want to logically separate the table from the cursor implementation. In addition, I want to hide both the table and cursor information from the user. However, doing so will generate the following compiler error
./table.h:10:1: error: unknown type name 'Cursor'
The alternative solution is to forward declare struct or put the struct declaration into the header files. But they are NOT very elegant.
Or I just don't know how to use forward declaration in a "better" way.
Is there any other way to achieve what I am trying to do here?
Any help will be greatly appreciated.
cursor.h
#ifndef CURSOR_H
#define CURSOR_H
#include "table.h"
typedef struct Cursor_t Cursor; //struct data hiding
//struct Table; //alternative solution: forward declaration
//struct Table* cursor_get_table(Cursor* cursor);
Table* cursor_get_table(Cursor* cursor);
#endif
cursor.c
#include "cursor.h"
typedef struct Cursor_t {
Table* table;
} Cursor;
//struct Table* cursor_get_table(Cursor* cursor) { //alternative solution
Table* cursor_get_table(Cursor* cursor) {
return cursor->table; //warning: incompatible pointer types returning 'Table *'...
}
table.h
#ifndef TABLE_H
#define TABLE_H
#include "cursor.h"
typedef struct Table_t Table; //struct data hiding
//struct Cursor; //alternative solution: forward declaration
//struct Cursor* table_start(Table* table);
Cursor* table_start(Table* table); //error: unknown type name 'Cursor'
#endif
table.c
#include "table.h"
typedef struct Table_t {
} Table;
//struct Cursor* table_start(Table* table) { //alternative solution
Cursor* table_start(Table* table) {
return 0;
}
main.c
#include <stdio.h>
#include "cursor.h"
#include "table.h"
int main() {
Table* table;
Cursor* cursor;
printf("hello world\n");
return 0;
}
Please note the main.c is just a test code. I am not trying to accomplish anything meaningful here.