473,770 Members | 6,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A dilemna with circular reference of headers file

JCB
Hi,

I have two C file which are referencing in a circular way.

To simplify I use an example with car :

Suppose I have a program for cars.

I have car.c, structure car and functions prototypes of car.c are in a
file car.h
I have also engine.c, a structure engine and functions prototypes of
engin.c are in a file engine.h.
Now the structure car in car.c use an engine structure, so I have to
write in car.c :

#include car.h
#include engine.h

.... (code)

Note :in car.h
struct car {
struct engine engine ;
etc...
} ;

Up to there, it's standard and there is no problem.
The problem is if I have to write a function in engine.c which
reference a car, in this case, I will have to write in engine.c :

#include engine.h
#include car.h

The problem is that engine depends on car and car depends on engine,
so either I write
(case 1)
#include engine.h
#include car.h
or
(case 2)
#include car.h
#include engine.h

it's not appropriated :

case 1
Function of car.h use the structure engine.h, so it's good, but
engine.h use structure car in car.h, not yet defined (because #include
car.h is after #include engine.h)
case 2:
Functions of engine.h that use structure car it's good, because
defined before, but in car.h , I need a reference to struct engine,
not yet defined...

Some people kwow what to do in this particular case ?
Nov 13 '05 #1
3 7276

On Tue, 8 Jul 2003, Pieter Droogendijk wrote:

On 8 Jul 2003 08:09:05 -0700, JCB wrote:

I have two C file which are referencing in a circular way. Some people kwow what to do in this particular case ?


car.h:
#ifndef __INCLUDED_CAR_ H__
#define __INCLUDED_CAR_ H__


Constraint violation. I used to be lax about this
case myself, until repeated admonitions in c.l.c gradually
changed my style. Now, I write instead:

#ifndef H_CAR
#define H_CAR

Shorter to type, and portable to all ISO C compilers.
Everyone wins.

Also N.B.: I have found that some circular references are
not fully defused until one has put all type declarations
above the other headers. I forget in what circumstances this
bit me, but now I also write:

engine.h:
#ifndef H_ENGINE
#define H_ENGINE

typedef struct engine Engine;

#include "car.h"

struct engine
{
Car *my_owner;
};

#endif

car.h:
#ifndef H_CAR
#define H_CAR

typedef struct car Car;

#include "engine.h"

struct car
{
Engine *my_engine;
};

#endif
-Arthur

Nov 13 '05 #2
In 'comp.lang.c', Pieter Droogendijk <gi*@binky.home unix.org> wrote:
#ifndef __INCLUDED_CAR_ H__
#define __INCLUDED_CAR_ H__


Please stop invading the implementation name space (_[A-Z_]xx identifiers
are reserved). The idiomatic form is:

#ifndef H_INCLUDED_CAR
#define H_INCLUDED_CAR

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3

On Tue, 8 Jul 2003, Derk Gwen wrote:

[stupid octothorpe quoting fixed]
jc*********@net courrier.com (JCB) wrote:

I have two C file which are referencing in a circular way.

I have car.c, structure car and functions prototypes of car.c are in a
file car.h
I have also engine.c, a structure engine and functions prototypes of
engin.c are in a file engine.h.
If car and engine are so incestuous that they need to know each other's
structures, you should move them into one file. They are tightly coupled
and the program organisation should reflect that.


IMHO, not if the concept of "engine" is distinct from the concept of "car"
(which in this trivial example it is). A car "has-a" engine, but so do
other things, and maybe someday you'll want to change your "engine" class
to work with "lawnmowers ", without having to weed out all the intermixed
declarations and definitions of "car"-related methods.

Of course, any beginner's textbook would tell you to use inheritance from
an "Engine-Powered Vehicle" class in the first place. But I don't listen
to that kind of textbook. ;-)

My advice: Keep the separate files. Just put them in the same directory.
IMHO.
You can couple them more loosely by using pointers to incomplete
structures and functions.
car.h
#ifndef car_h
#define car_h
#include "engine.h"
typedef struct car car;
t functionname(en gine *...);
t functionname(.. .);
...
#endif
Ahh. *This* is where the problem I mentioned earlier bites people.
Look at what happens when this file is preprocessed:
#ifndef car_h
#define car_h
#include "engine.h" "car_h" is not defined, so "car_h" gets defined.
Then we #include "engine.h" as follows: #ifndef engine_h
#define engine_h
#include "car.h" "engine_h" is not defined, so "engine_h" gets defined.
Then we #include "car.h" as follows: #ifndef car_h "car_h" is defined, so we skip to the corresponding #endif ....and return to processing "engine.h".
typedef struct engine engine;
Now we have a typedef for "engine". Good.
t functionname(ca r *...);


Now we have a function taking a "car *". Bad.
The identifier "car" has not been declared to mean
anything anywhere. The compiler chokes.

The solution to this problem is left as an exercise
for Derk. (I explained it, albeit less lucidly, earlier
in the thread.)

-Arthur

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
410
by: Vera | last post by:
I have two assemblies that each consist of several classes. Each object instantiated from those classes can have one or more child- and/or parentobjects that are also instantiated from those classes. Most relationships exist within one assembly, but relationships between assemblies may sometimes occur. For each relationship, I have to make sure that an object from Class1 knows that it can have an object from Class2 as one of its children....
2
1945
by: John E. | last post by:
How can I compile two projects with a circular reference while giving them a strong name? There is a project that we have that has two components that reference each other e.g. A<->B thus creating A.dll and B.dll. I have a project (e.g. R with an assembly name of Com.MyCompany.R.dll) that has to use this mess and access A. So, R->A. Well, Com.MyCompany.R.dll is going in the GAC. It is set for strong name (entry in the assembly...
16
2839
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more semi-circular definitions and
6
2830
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself stumped again with this little love triangle. Here is some background: m_commands.h defines - struct command_callbacks, which contains - a struct conn * reference - struct command, which contains - a struct command_callback reference
4
3284
by: Henke | last post by:
I have this scenario. public class A { public int numbers; public class A() { }
2
480
by: Mike | last post by:
I ran into a problem with circular dependencies. My code looks somthing like: using B namespace A { class formA { void foo() {
12
7063
by: Frank Rizzo | last post by:
I have a circular reference between 2 classes in the same project (i.e. each class refers to the other). The app runs fine and I am seeing no issues, which kind of surprised me. Are there any issues that I am not seeing (performance wise or garbage collection wise) with circular references? Thanks.
6
5077
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson) in search (no record) mode. When the search is executed, frmperson calls frmsearchresult (dim f as new frmsearchresult) which is a listing of persons. From frmsearchresults, frmperson is called (dim f as new frmperson) with the resulting...
3
2925
by: =?Utf-8?B?UGF1bCBIYWxl?= | last post by:
Moving all User Controls to a single directory has solved my problem - Thanks Eliyahu. That said, I still got one Circular ref error yesterday, rebuilt again and the build was fine? Far far better than the amount of errors I was originally getting on builds before I consilidated by UC's though :-) "Paul Hale" wrote:
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.