Re: header file help......



Define "use". Do you mean "Can I declare objects of the type declared in the first header
file in the second?" and the answer is "yes". If the question is "can I use the name as
the name of a structure in another header file" and the answer is "if the two files are
never included in the same compilation unit, yes, but this would be an Exceptionally Bad
Idea"

Consider a.h
typedef struct {
....stuff here
} A, *PA;

consider b.h

typedef struct {
A a1;
A a2;
} B, *PB;

this is valid but only if a.h has already been included. Therefore, two things need to be
added:

in a.h, add the line
#pragma once

so that multiple includes will not generate errors of multiply-defined symbols. In b.h it
is necessary to write

#include "a.h"

before the usage. The general rule is that every header file ought to have
#pragma once

in it, so b.h really should have that line added also. And every header file should
explicitly include everything it needs for correct compilation. So a file

c.h:

#pragma once
#incldue "a.h"
typedef struct {
A a1;
A a2;
} C, *PC;

Now, if your program app.cpp has

#include "stdafx.h"

#include "b.h"
#incldue "c.h"

it will compile correctly because a.h is already included by BOTH b.h and c.h, but because
of the #pragma once it is actually read only once. Note that if you invert the order to
be

#include "c.h"
#incldue "b.h"

then it wil STILL compile correctly because a.h is still included independent of the
order.
joe

On Mon, 02 Jul 2007 23:47:22 -0700, raju2000myin@xxxxxxxxx wrote:



am delare one structure in one header file.........
can i use that structure name in another header file......... is it
possible.
If yes how can i use that..........
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: About header files
    ... I put in the header file: all function prototypes with keyword extern ... and the declarations of the structs, which are defined in the main ... struct type definition goes in header ... struct type definition goes in module's .c file ...
    (comp.lang.c)
  • [PATCH] remove unnecessary #includes from <linux/fs.h>
    ... >> everything being rebuilt just because one header file was touched. ... struct nameidata; ... extern int is_subdir ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Newbie question
    ... >>> What I've done is declare a struct in a header file, ... >> Declaring structs, typedefs, function prototypes etc yes, defining ... >the header file and not again referenced in the code. ... >the header file in each code file is a duplicate declaration, ...
    (comp.lang.c)
  • [PATCH 3/11] perfmon2 patch for review: new generic header files
    ... +#ifdef CONFIG_PERFMON ... * PMD/PMC return flags in case of error ... +struct pfarg_ctx { ... * This part of the header file is meant for kernel level code only including ...
    (Linux-Kernel)
  • [PATCH 3/11] 2.6.17-rc5 perfmon2 patch for review: new generic header files
    ... +#ifdef CONFIG_PERFMON ... * PMD/PMC return flags in case of error ... +struct pfarg_ctx { ... * This part of the header file is meant for kernel level code only including ...
    (Linux-Kernel)