473,770 Members | 4,718 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
14 22370
Malcolm wrote:
"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 waters are muddied further when the compiler supports some form of
pre-compiled headers, where a global header that includes all headers
can save a significant amount of compile time.

--
Ian Collins.
Jan 28 '06 #11
Malcolm wrote:
That means that every file in the program includes "world.h" and no other
headers, and any sense of hierarchy is lost.


I don't understand what hierarchy has to do with it here. Include when
you need a definition or declaration to be present - otherwise don't.
Perhaps move your globally used types to a world_types.h file or
otherwise equivalent.

You won't get any coding done if you fret about the hierarchy of header
file includes. It doesn't mean make it messy and unorganized - but
there's a point where one just has to move on and start on the actual
heavy lifting. Include-guard everything.

Jan 28 '06 #12
On Sat, 28 Jan 2006 00:55:14 +0000 (UTC), in comp.lang.c , "Malcolm"
<re*******@btin ternet.com> wrote:

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


I believe that since they're all pointers, you can actually declare
them as incomplete types, and only supply a definition when you need
it.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 28 '06 #13
On 27 Jan 2006 20:19:24 -0800, in comp.lang.c , "clayne"
<cl****@anodize d.com> wrote:
Malcolm wrote:
That means that every file in the program includes "world.h" and no other
headers, and any sense of hierarchy is lost.


I don't understand what hierarchy has to do with it here. Include when
you need a definition or declaration to be present - otherwise don't.


Malcolm's point was that one struct that is universally used, relies
on pointers to other structs which are not always used. Thus his
universal header included all other headers and there was no easy way
to tell what features a given module actually used.

I actually think he can get round this via incomplete types, but its a
valid issue.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 28 '06 #14

"Mark McIntyre" <ma**********@s pamcop.net> wrote
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


I believe that since they're all pointers, you can actually declare
them as incomplete types, and only supply a definition when you need
it.

That's what I'm currently doing.
I still don't really like it, because then we can't modify WORLD to include
a

struct cell current_cell;

Also, the world.c file itself is still topsy-turvy. Probably the solution is
to put the trivial WORLD functions like "getwidth() " in one file, and the
main ones like "run_for_cy cle" in another.
Jan 29 '06 #15

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

Similar topics

3
14265
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
7066
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
1743
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
28675
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
12119
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
9
4043
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
1674
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
7911
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
2542
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
1551
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
9595
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
10232
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...
0
10059
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...
1
10008
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,...
1
7420
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
6682
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
5313
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
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
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

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.