Using typedef struct in multiple files - c

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?

Related

C - Unknown type name

I need to build a "social network" for college, but I always get unknown type name 'List' while compiling. I removed a lot of functions from my headers, but I still get the same error and I don't know why.
I've got 3 headers:
My friend's header
#ifndef FRIEND_H
#define FRIEND_H
#include "ListHeadTail.h"
typedef struct Friend{
int id;
struct Friend *nextFriend;
}Friend;
void printFriends(List *l);
void removeFriend(List *l);
void addFriend(List *l);
#endif /* FRIEND_H */
My list header:
#ifndef LISTHEADTAIL_H
#define LISTHEADTAIL_H
#include "Student.h"
typedef struct pStudent{
struct pStudent *ant;
Student *s;
struct pStudent *prox;
}pStudent;
typedef struct list{
pStudent *head;
pStudent *tail;
}List;
void startList(List *l);
void printList(List *l);
void freeList(List *l);
#endif /* LISTHEADTAIL_H */
My student's header
#ifndef STUDENT_H
#define STUDENT_H
#define MAX 51
#include "Friend.h"
#include "ListHeadTail.h"
typedef struct Student{
int id;
char name[MAX];
Friend *friends;
}Student;
Student* readStudent ();
void printStudent(Student* a);
void changeData(List *l);
#endif /* STUDENT_H */
My main:
#include <stdio.h>
#include <stdlib.h>
#include "ListHeadTail.h"
#include "Friend.h"
#include "Student.h"
int main(int argc, char** argv) {
List l;
startList(&l);
freeList(&l);
return (EXIT_SUCCESS);
}
Thanks for reading.
Here's the (first) error I get when I try to compile this set of files:
$ cc main.c
In file included from main.c:4:
In file included from ./ListHeadTail.h:4:
In file included from ./Student.h:6:
./Friend.h:11:19: error: unknown type name 'List'
void printFriends(List *l);
Look at the file names and line numbers. Note that at ListHeadTail.h line 4, you've already defined LISTHEADTAIL_H, but you haven't yet reached the actual declaration of List. You then go into Student.h, and from there into Friend.h. That includes ListHeadTail.h again -- but since LISTHEADTAIL_H is already defined, this include does nothing. So you continue through Friend.h with no declaration of List, and therefore get an error on the declarations that reference it.
As noted by #lurker in their comment, the basic issue here is circular dependency, and a simple fix is forward declaration. In this case, you could simply modify Friend.H, replacing #include "ListHeadTail.h" with typedef struct list List;.
But to me this is a bit hacky. If you shift the order of includes somewhere, the build might break again.
I think the real problem is that the declarations of the functions (printFriends, etc.) don't belong in Friend.h; they belong in ListHeadTail.h. The functions have nothing to do with the Friend type. Sure, they have "Friend" in their names, but the only type referenced in the declarations is List. So they belong in ListHeadTail.h. Same goes for the changeData function in Student.h.
In an object-oriented design (say, in Java), these functions would all probably be methods of the List class, and would be declared in that class's source file.

Reusing C code, giving struct multiple names

I have two header files
A.h
struct A { ... };
function declarations which use A
B.h
function declarations which use the struct A here, and have functions which also use A.
However, I want to call "A" B here. How can I do this? I want all the
functions in here use
struct B, which I want to be exactly the same as struct A since
An example of what I "want" to do, but uses defines and is probably the wrong way of doing things: (Note, it works perfectly how I want it to, but I don't think I should be using defines for this purpose, there is probably a better way of doing things)
A.h
#ifndef A_H
#define A_H
struct A {int x;};
void A_DoSomething(struct A* a);
#endif
B.h
#ifndef B_H
#define B_H
#include "A.h"
#define B A
void B_DoSomething(struct* B b) { A_DoSomething(b); }
#endif
So is there a way to do what I want without using define? I want to do this so I can reuse code. I.e., A is a linked list, B is a stack. I can completely define my stack data structure from a linked list.
EDIT: So basically B and A are equivalent, but for my B.h/B.c file, and any files using B.h, I just want to call the structure "B" and not "A"
I would use typedef and use 3 h-files to separate the common data structure from A and B. Something like:
MyNode.h:
#ifndef MyNode_H
#define MyNode_H
typedef struct Node
{
void *data;
struct Node *next;
} Node;
#endif
A.h:
#ifndef A_H
#define A_H
#include "MyNode.h"
typedef Node A;
/* Declare functions for implementing a linked list using type A */
#endif
B.h:
#ifndef B_H
#define B_H
#include "MyNode.h"
typedef Node B;
/* Declare functions for implementing a stack using type B */
#endif
Main.c:
#include <stdio.h>
#include "A.h"
#include "B.h"
int main(void) {
/* Here A and B can be used as types, example: */
A list = {NULL, NULL};
B stack = {NULL, NULL};
return 0;
}

Typedef usage from one header file in second header file

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

Typedef in header file not visible to another file that includes it

I am encountering the following problem in C:
I declare a typedef for a struct in a headerfile ("mep.h")
#ifndef MEP_H
#define MEP_H
typedef struct Mep_tag Mep;
<other stuff declared here>
#endif
I use another headerfile ("mep_types.h") that includes "mep.h", defines the struct "Mep_tag" and uses the "Mep" type name:
#ifndef MEP_TYPES_H
#define MEP_TYPES_H
#include "mep.h"
#include "file1.h"
struct Mep_MsgElement_tag
{
const Mep * MsgCh;
};
struct Mep_tag
{
<stuff in here>
};
#endif
For some reason when this compiles, I get the following error:
"mep_types.h: error: unknown type name "'Mep'".
However, if in the "mep.h" I place the typedef outside the ifndef guard like this...
typedef struct Mep_tag Mep;
#ifndef MEP_H
#define MEP_H
<other stuff declared here>
#endif
... the "Mep" type name is visible in "mem_types.h".
Might someone know how this could happen?

Includes causing struct alias to not be visible?

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

Resources