Reusing C code, giving struct multiple names - c

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;
}

Related

How can I avoid redefinition of types while including multiple header files?

I'm working on recompiling a C project and I'm not sure how do I fix this problem in a right way.
Here is a situation -
a.h
#ifndef A_H
#define A_H
typedef int INT;
// other variables and function definition
#endif
b.h
#ifndef B_H
#define B_H
typedef int INT;
// other variables and function definition
#endif
main.c
#include "a.h"
#include "b.h"
int main()
{
INT i = 10;
return 0;
}
The error I get in Linux with gcc:
In file included from ./main.c,
./b.h:<linenumber>: error: redefinition of typedef ‘INT’
a.h.h:<linenumber>: note: previous declaration of ‘INT’ was here
I have to include both headers due to other variables and functions. I haven't written this code, but this seems to compile in my Solaris environment which is strange. What can I do to fix this ?
Probably the native compiler on Solaris accepts that you can redefine a typedef (probably provided that the new typedef is identical to the previous one which is the case here).
I'd introduce another header file mytypes.h like this:
mytypes.h
#ifndef MYTYPES_H
#define MYTYPES_H
typedef int INT;
#endif
Include mtypes.h whereever INT is used, possibly even in main.c:
a.h
#ifndef A_H
#define A_H
#include "mytypes.h" // can be removed if INT is not used in a.h
// other variables and function definition
#endif
b.h
#ifndef B_H
#define B_H
#include "mytypes.h" // can be removed if INT is not used in b.h
// other variables and function definition
#endif
main.c
#include "a.h"
#include "b.h"
#include "mytypes.h" // not really necessary because it's already included
// via a.h and b.h, but still good practice
int main()
{
INT i = 10;
return 0;
}
If you are allowed to change the library code or compiler options then Michael Walz's answer is the way to go
In the unfortunate case that it's not changeable then it can be worked around by renaming before including the header then undefine it
#define INT INT_A
#include "a.h"
#undef INT
#define INT INT_B
#include "b.h"
#undef INT
Now just use INT_A for all the interfaces in a.h instead of INT. Same to INT_B in b.h

Declaration of the two structures and use one in the second

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

Declaring the various typedefs in a stack ADT

Here's my problem: I need to implement a FIFO/LIFO list stack as ADT species 1. My program is modular and it have an item.h module:
#ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED
typedef struct
{
char stringa[20];
int numero;
} Item;
#endif // ITEM_H_INCLUDED
The head.h module:
#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
void QUEUEinit();
int QUEUEempty();
void QUEUEput_top(Item);
void QUEUEput_bottom(Item);
Item QUEUEget_top();
Item QUEUEget_bottom();
#endif // HEAD_H_INCLUDED
The main.c and data.c; what i need is how i declare a QEUEnode struct and where.
Thank you for the help :)
Since none of your QUEUE* functions receive a QUEUEnode *, you can hide it in the head.c file, along with the QUEUEnode root; that they operate on.
If you want to use multiple queues, then it should probably be in the head.h file so they can be created in main.c. For this, you'll also need to modify the functions to accept a queue to operate on.

Using typedef struct in multiple files

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?

Transferring a list to a class

I have a list of checkpoints and then a run a function. I originally built this list in that function, but now I have to build it outside. The problem is that I cannot include checkpoint.h in the class that implements that function because checkpoint.h returns a structure of the type of that class. The initial list was declare in class.c globally. How can I transfer the list created outside into class so I can use it?
So I have this header, turing_machine.h:
#ifndef __TURING_MACHINE__
#define __TURING_MACHINE__
#include "tape.h"
#include "alphabet.h"
#include "symbol_table.h"
...
#endif
and the checkpoint.h header defining the checkpoint_list class:
#ifndef __CHECKPOINT_H__
#define __CHECKPOINT_H__
#include "turing_machine.h"
...
#endif
So I want to send to a function from turing_machine.h a list of structures checkpoint but I can't modify anything because that's how the classes must stay.
I have also turing_machine.c:
#include "turing_machine.h"
#include "checkpoint.h"
#include "symbol_table.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
checkpoint_list *c;
So at the beginning I created in turing_machine that list, c, but now I must create it outside and I have to initialize that list c but I don't know how. I hope this is more clear.
I used the term class wrong; I have just .c and .h files.
Reading between the lines, somewhat, I think your trouble is that you have 'mutually referencing' structures.
The way to work around this is with an incomplete type definition:
typedef struct checkpoint_list checkpoint_list;
You can then use that inside turing_machine.h:
#ifndef TURING_MACHINE_H_INCLUDED
#define TURING_MACHINE_H_INCLUDED
#include "tape.h"
#include "alphabet.h"
#include "symbol_table.h"
typedef struct checkpoint_list checkpoint_list;
typedef struct turing_machine
{
...
} turing_machine;
extern checkpoint_list *tm_function(turing_machine *);
extern turing_machine *tm_create(const char *);
#endif
And, inside checkpoint.h, you can write:
#ifndef CHECKPOINT_H_INCLUDED
#define CHECKPOINT_H_INCLUDED
#include "turing_machine.h"
/* No typedef here in checkpoint.h */
struct checkpoint_list
{
...
};
extern checkpoint_list *cp_function(const char *);
extern turing_machine *cp_machine(checkpoint_list *);
#endif
This technique is recognized and defined by the C standard (C90, let alone C99 or C11).
Note that I've also renamed the include guards; names that start with double underscore are reserved for 'the implementation' (meaning the C compiler and its libraries), and you should not invent and use such names in your own code.

Resources