In my C project, I have a header file with a declaration of a struct (with an alias) and a header file with functions that accept that struct as an argument (using the alias). I am receiving the error expected ')' before '*' token on the function. From researching, I think this indicates that the alias is not visible in the function's namespace. I have a fairly convoluted include web that I believe may be causing this. Here is a simplified example.
Struct declaration header:
#ifndef STRUCTHEADER_H_
#define STRUCTHEADER_H_
#endif /* STRUCTHEADER_H_ */
#ifndef STRUCTFUNCTIONS_H_
#include "structFunctions.h"
#endif
struct myStruct{
};
typedef struct myStruct mys;
Struct function header:
#ifndef STRUCTFUNCTIONS_H_
#define STRUCTFUNCTIONS_H_
#endif /* STRUCTFUNCTIONS_H_ */
#ifndef STRUCTHEADER_H_
#include "structHeader.h"
#endif
void func(mys* s);
main.c:
#ifndef STRUCTHEADER_H_
#include "structHeader.h"
#endif
int main(int argc, char* argv[]){
return 0;
}
However, when I change main.c to this:
#ifndef STRUCTFUNCTIONS_H_
#include "structFunctions.h"
#endif
int main(int argc, char* argv[]){
return 0;
}
the error goes away. Am I using include wrong?
You need to change structHeader.h so that it declares the structure before it includes structFunctions.h, because the function definition needs to refer to the alias. In C, you can only refer to names that have been previously declared.
#ifndef STRUCTHEADER_H_
#define STRUCTHEADER_H_
#endif /* STRUCTHEADER_H_ */
struct myStruct{
};
typedef struct myStruct mys;
#ifndef STRUCTFUNCTIONS_H_
#include "structFunctions.h"
#endif
Related
I declared a struct in main.c file and i have functions in func1.c func2.c func1.h func2.h files.
main.c:
#include <stdio.h>
#include "func1.h"
#include "func2.h"
struct mystruct{
someting
};
main(){
struct mystruct var ={...};
myfunc(var);
}
func1.h:
void myfunc(struct mystruct);
And definition is in func1.c. Similar for func2 files. I got compilation errors, it is obvious that problem is header file dont have declaration of mystruct and so cant use it.
So what is the way to overcome that problem? Adding a new header file for struct or using extern keyword what i read. What is approprite choice i couldnt figure out at that point.
struct mystruct{
//something
};
Should be defined in a header file which all your c files that need it has access to.
mytypes.h
#ifndef mytypes_h
#define mytypes_h
struct mystruct{
int something;
};
#endif
func1.h
#ifndef func1_h
#define func1_h
#include "mytypes.h"
void myfunc(struct mystruct);
#endif
func1.c
#include "func1.h"
void myfunc(struct mystruct){
//do soemthing with struct
}
Then your main can include func1.h which already mytypes.h
main.c
#include "func1.h"
int main(){
}
Basically, I need to have typedef in one header file and use it on another header.
myType.h:
#ifndef deque_H
#define deque_H
#include "deque.h"
typedef int intDef;
#endif
deque.h:
#ifndef deque_H
#define deque_H
#include "myType.h"
typedef struct dequeNode *link;
struct dequeNode{
intDef data;
link next;
//count
};
#endif
I want to use intDef in deque.h, but I get project error \deque.h|6|error: unknown type name 'intDef'|
Does anybody have a clue what's wrong? myType.h is in the same project.
You prevent your myType.h from ever being executed, since you use the same flag as in the other file. You need to choose any other symbol and check if it's defined:
#ifndef myType_H
#define myType_H
typedef int intDef;
#endif
I'm having trouble finding my error. Here is a definition in structures.h
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
And here is in functions.c where I include header and try to use the type book_t
#include "structures.h"
void load_book(book_t *book) {
}
But I get this error
functions.c:10:16: error: unknown type name ‘book_t’
void load_book(book_t *book) {
^
Edits with more code below:
In my main file I order my .h files like so
#include "structures.h"
#include "functions.h"
structures.h
#ifndef STRUCTURES_H
# define STRUCTURES_H
typedef struct bank_account {
char *name;
int amount;
} bank_account_t;
typedef struct transaction {
char *name;
int amount;
} transaction_t;
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
#endif
function.c
#include <stdio.h>
#include "functions.h"
#include "structures.h"
#include "bank_account.h"
#include "transaction.h"
void load_book(book_t *book) {
}
void init_book() {
}
bank_account.h
#ifndef BANK_ACCOUNT_H
# define BANK_ACCOUNT_H
void init_new_bank();
void deinit_new_bank();
#endif
transaction.h
#ifndef TRANSACTION_H
# define TRANSACTION_H
#endif
I think the problem must be in functions.h (which is not included in the original post).
functions.h
#ifndef FUNCTIONS_H
# define FUNCTIONS_H
/* [MarkU] required: include definition of book_t */
#include "structures.h"
void load_book(book_t *book);
void init_book();
#endif
Without the #include structures.h there is no definition of the boot_t type.
Built and verified with mingw32-gcc 4.7.2. Omitting the #include, I see the error message.
In functions.c change the order of those:
#include "functions.h"
#include "structures.h"
to be
#include "structures.h"
#include "functions.h"
The subtile thing is that the error message origins from functions.c not from functions.h.
Assuming the protoytpe to load_book(book_t *) in functions.h, it needs to know about book_t.
So the optimal solution to this would be to include structures.h into functions.h (as also already pointed out by MarkU's answer).
Lesson learned: Always (and only) include what you need and where you need it. Avoid (subtile) dependencies.
I've got two structures in two different header files, let's say:
header1.h:
#ifndef HEADER1_H
#define HEADER1_H
#include "header2.h"
typedef struct
{
Struct2 s;
} Struct1;
#endif
header2.h:
#ifndef HEADER2_H
#define HEADER2_H
#include "header1.h"
typedef struct
{
Struct1* s;
} Struct2;
#endif
As you can see i declare Struct2 in Struct1 and pointer to Struct1 in Struct2. Obviously when i try to compile this it gives me an error: unknown type name ‘Struct1’ or 'Struct2'. Is there any way to do this or i must change my conception?
If you really want to do this, you can, you just need to use partial declarations before really declaring each structure:
header1.h
#ifndef _HEADER1_H
#define _HEADER1_H
#include "header2.h"
struct struct1
{
struct struct2 s2;
};
#endif
header2.h
#ifndef _HEADER2_H
#define _HEADER2_H
struct struct1;
struct struct2
{
struct struct1 *s1;
};
#endif
I have a typedef struct declared in one my headers. Its associated C file can find the typedef, but other headers have trouble reading it.
// In projectiles.h I have
#ifndef PROJECTILES_H_
#define PROJECTILES_H_
struct TheProjectile { };
typedef struct TheProjectile Projectile;
#endif /* PROJECTILES_H_ */
In physics.h I want to use Projectile
#ifndef PHYSICS_H_
#define PHYSICS_H_
#include "projectiles.h"
struct TheProjectile;
void set_Current_Angle(Projectile* PI);
#endif /* PHYSICS_H_ */
However, in Eclipse I keep getting "expecting ) before PI" error. Without typedef it does work fine. What am I doing wrong?