473,671 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#include in header file for size_t

Imagine I have a structure with a size_t member:
/* foo.h */
struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);
Now I #include it where needed
/* foo.c */
#include "foo.h"
void make_foo(struct foo *p) {
p->bar = "immutable string";
p->barlen = 16;
}
If I compile these files now I get a `parse error before "size_t"'.
To compile I need to #include <stdlib.h>.

As foo.c doesn't need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn't include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?
[ It doesn't matter, but for completeness sake, I'm using gcc 3.3.5 ]
[ compile command-line: gcc -W -Wall -std=c89 -pedantic -c foo.c ]

--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Jan 27 '06 #1
14 22357
Pedro Graca <he****@dodgeit .com> writes:
Imagine I have a structure with a size_t member:
/* foo.h */
struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);
Now I #include it where needed
/* foo.c */
#include "foo.h"
void make_foo(struct foo *p) {
p->bar = "immutable string";
p->barlen = 16;
}
If I compile these files now I get a `parse error before "size_t"'.
To compile I need to #include <stdlib.h>.

As foo.c doesn't need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn't include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?

There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.

Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:

=============== =============== ==========
#ifndef FOO_H
#define FOO_H

struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);

#endif /* FOO_H */
=============== =============== ==========

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 27 '06 #2
Keith Thompson said:
There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.

Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:

=============== =============== ==========
#ifndef FOO_H
#define FOO_H

struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);

#endif /* FOO_H */
=============== =============== ==========


You forgot to #include <stddef.h> :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 27 '06 #3
> Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:


Some of them don't: <assert.h>, for one. The only time include guards
shouldn't really be used is if a header can be included multiple times
in the same file and have different effects each time. For example, I
once designed a dynamic array header that I didn't put include guards
in because it needed things defined to work and could be included
multiple times in the same file for dynamic arrays of different types.

Still, it's the exception, not the rule. If an include file should not
have include guards, the programmer should say so in the comments.

Gregory Pietsch

Jan 27 '06 #4
Keith Thompson wrote:
Pedro Graca <he****@dodgeit .com> writes:

<snip>
Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?


I must have misunderstood something I read.
The inexistence of such a rule solves my 'problem' :)

--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Jan 27 '06 #5
Richard Heathfield wrote:
[ snip: stuff with references to <stdlib.h> lost ]
You forgot to #include <stddef.h> :-)


Ah! I've peeked into /usr/include/stdlib.h and found out it does a
#include <stddef.h>

My hard disk thanks you :)

.... or is there another reason to choose <stddef.h> over <stdlib.h>?

--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Jan 27 '06 #6
On 2006-01-27, Pedro Graca <he****@dodgeit .com> wrote:
Imagine I have a structure with a size_t member:

Now I #include it where needed

If I compile these files now I get a `parse error before "size_t"'.
To compile I need to #include <stdlib.h>.

As foo.c doesn't need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn't include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?


I've never heard of such a rule, but size_t also "lives" in stddef.h,
which doesn't declare any functions.
Jan 27 '06 #7
On 2006-01-27, Gregory Pietsch <GK**@flash.net > wrote:
The only time include guards shouldn't really be used is if a header
can be included multiple times in the same file and have different
effects each time.


I wouldn't call that a header - i'd maybe name it .inc instead. Same
with includable files that define data structures [unless it has a more
specific suffix associated with it, such as .xpm]
Jan 27 '06 #8
On 2006-01-27, Pedro Graca <he****@dodgeit .com> wrote:
Richard Heathfield wrote:
[ snip: stuff with references to <stdlib.h> lost ]
You forgot to #include <stddef.h> :-)
Ah! I've peeked into /usr/include/stdlib.h and found out it does a
#include <stddef.h>


That's not guaranteed - all that's guaranteed is that size_t (, wchar_t,
and NULL) is made available by both.

My hard disk thanks you :)

... or is there another reason to choose <stddef.h> over <stdlib.h>?


You don't need the stuff in stdlib.
Jan 27 '06 #9

"Keith Thompson" <ks***@mib.or g> wrote .
Pedro Graca <he****@dodgeit .com> writes:

Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?

There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.

I've never found a good answer to this one.

If you follow a policy of never allowing nested inclusions, then you have a
list of dependencies at the top of each source file.
You also have a list, in reverse order, of the files in call hierarchy.

The problem is that quite often, in video games, you need a "world".
Everything depends on the world, but it also contains everything.
Therefore my function

move_invader(IN VADER *baddy, WORLD *wld)

take the world as a parameter, becase the invader needs to update its
position within it.
We will want all the invader functions, like invader_fire, invader_die,
invader_land_an d_take_over_Ear th in the same invader.c file.

However the world looks like this

typedef struct
{
INVADER *invaderlist;
int width;
int height;
CELL *cells;
SHIP *goody;
} WORLD;

So the easiest thing is to include the definitions of INVADER, CELL, and
SHIP as nested includes in world.h

That means that every file in the program includes "world.h" and no other
headers, and any sense of hierarchy is lost.
Jan 28 '06 #10

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

Similar topics

3
14243
by: JKop | last post by:
I'm writing a ".cpp" and ".hpp" combination. The ".hpp" just contains one sole function declaration. The ".cpp" makes use of the function "std::strlen" and the type "std::size_t".
6
7061
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this header file. The function is stored in C:\temp\myfun.c int func(){ return 1;
6
1736
by: candy | last post by:
hi all, I just want to know that whether the C header files( like stdio.h,etc which the compiler provides) just contains the function declarations or they also contain some additionalinformation like where to look in the memory for the defintions of the standard functions like scanf(char*c,...)).
5
28663
by: Angel | last post by:
Is there some way to add a C-header file into a C# project? The problem is that this .h file contains several complex structures (over 20) that are used by some unmanaged code. These functions receive these defined structs as parms and modify them during execution. Basically, it'd be something siomple like this (but in C#): #include <parms.h> W32_PARM parm;
13
12103
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
9
4040
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
3
1667
by: andrejohn.mas | last post by:
Hi, I am writing a C++ program that needs to include a C header file. The problem I am having is that a couple of the functions have 'new' as parameter name: int kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval); int userland_sysctl(struct proc *p, int *name, u_int namelen, void
3
7907
by: Dv | last post by:
I have a API lib (written in C/C++) that can be used by C/C++ project. Now, I'm adding support to C# project. I changed the Lib to DLL. This is easy. However, I have no idea how to deal with the structures and constants that used to be defined in the header file of the lib. Where should I define them that they can be commonly used by the DLL, C ++ and C#, and also is readable to their developers from their IDEs?
11
2529
by: subramanian100in | last post by:
Suppose the following is in Test.h #ifndef TEST_H #define TEST_H #include <iostream> #include <string> using namespace std;
1
1548
by: pralu | last post by:
hi i have made one header file named struct EventLogEntry.h can u tell me how can i add it to my main source code ...in visual c
0
8485
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
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8677
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
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
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
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.