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

Home Posts Topics Members FAQ

array in include

Hi,
I've an array in an include that is used everywhere in the project.

So to avoid the problem of declaration I made the following solution:

ifndef __UNWALKABLETIL ES__
#define __UNWALKABLETIL ES__

#define UNWALKABLETILES 16 /*!< Number of elements in UnwalkableTiles
array. */

/** List of unwalkable tiles. */
char rgUnwalkableTil es[] =
{
18, 19, 20, 21, 22, 23, 24, 25, 55, 56, 57, 60, 61, 62, 63, 64
};
#endif

But it doesn't works.
Why?
Tnx

Jul 16 '06 #1
13 1931
Salvatore Di Fazio wrote:
Hi,
I've an array in an include that is used everywhere in the project.

So to avoid the problem of declaration I made the following solution:

ifndef __UNWALKABLETIL ES__
#define __UNWALKABLETIL ES__

#define UNWALKABLETILES 16 /*!< Number of elements in UnwalkableTiles
array. */

/** List of unwalkable tiles. */
char rgUnwalkableTil es[] =
{
18, 19, 20, 21, 22, 23, 24, 25, 55, 56, 57, 60, 61, 62, 63, 64
};
#endif
Your program's modules can *declare* rgUnwalkableTil es[]
as often as you like, but must *define* it exactly once. The
usual way of doing this is to put the declaration in a header
file that you #include in all the source modules:

/* tiles.h */
#define UNWALKABLETILES 16
extern char rgUnwalkableTil es[UNWALKABLETILES];

.... and then place the definition in exactly one source file:

/* somefile.c */
#include "tiles.h"
char rgUnwalkableTil es[] = { ... as above ... };

Note that somefile.c #includes tiles.h; this is a very
good idea because it lets the compiler "see" the declaration
and the definition at the same time, which means it can detect
any inadvertent mis-matches.

--
Eric Sosman
es*****@acm-dot-org.invalid

Jul 16 '06 #2
"Salvatore Di Fazio" <sa************ ***@gmail.comwr ites:
I've an array in an include that is used everywhere in the project.

So to avoid the problem of declaration I made the following solution:

ifndef __UNWALKABLETIL ES__
#define __UNWALKABLETIL ES__

#define UNWALKABLETILES 16 /*!< Number of elements in UnwalkableTiles
array. */

/** List of unwalkable tiles. */
char rgUnwalkableTil es[] =
{
18, 19, 20, 21, 22, 23, 24, 25, 55, 56, 57, 60, 61, 62, 63, 64
};
#endif

But it doesn't works.
Why?
Eric Sosman already answered the question you asked, so I'll answer
some questions you didn't ask.

Include guards are an excellent idea, but you should avoid identifiers
starting with underscores; many of them are reserved to the
implementation. (There are rules that depend on whether the second
character is another underscore, an upper case letter, or a lower case
letter, but it's not really worth remembering the details.)

If the include guard is supposed to refer to the header, a good
convention is "H_" followed by the header name; for example:

#ifndef H_UNWALKABLETIL ES
#define H_UNWALKABLETIL ES
....
#endif

Also, long run-on identifiers like UNWALKABLETILES are difficult to
read. I suggest changing it to UNWALKABLE_TILE S.

--
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.
Jul 16 '06 #3

Eric Sosman wrote:
Your program's modules can *declare* rgUnwalkableTil es[]
as often as you like, but must *define* it exactly once.
Why?
I don't must define it exactly ...
The usual way of doing this is to put the declaration in a header
file that you #include in all the source modules:

/* tiles.h */
#define UNWALKABLETILES 16
extern char rgUnwalkableTil es[UNWALKABLETILES];

... and then place the definition in exactly one source file:

/* somefile.c */
#include "tiles.h"
char rgUnwalkableTil es[] = { ... as above ... };

Note that somefile.c #includes tiles.h; this is a very
good idea because it lets the compiler "see" the declaration
and the definition at the same time, which means it can detect
any inadvertent mis-matches.
But I prefere keep it in a header file.

Tnx

Jul 19 '06 #4


Salvatore Di Fazio wrote On 07/19/06 14:40,:
Eric Sosman wrote:

> Your program's modules can *declare* rgUnwalkableTil es[]
as often as you like, but must *define* it exactly once.


Why?
I don't must define it exactly ...
I misspoke: You do not need to define the array at all --
provided you never use it. Section 6.9, paragraph 5:

[...] If an identifier declared with external linkage
is used in an expression (other than as part of the
operand of a sizeof operator whose result is an integer
constant), somewhere in the entire program there shall
be exactly one external definition for the identifier;
otherwise, there shall be no more than one.

So if you intend to use any of the values in rgUnwalkableTil es[]
you must provide exactly one definition of the array; exactly
one compiled module must contain the array's definition. (If
you don't use the array, it's all right not to define it at all
but you still mustn't define it twice.)
>>The usual way of doing this is to put the declaration in a header
file that you #include in all the source modules:

/* tiles.h */
#define UNWALKABLETILES 16
extern char rgUnwalkableTil es[UNWALKABLETILES];

... and then place the definition in exactly one source file:

/* somefile.c */
#include "tiles.h"
char rgUnwalkableTil es[] = { ... as above ... };

Note that somefile.c #includes tiles.h; this is a very
good idea because it lets the compiler "see" the declaration
and the definition at the same time, which means it can detect
any inadvertent mis-matches.


But I prefere keep it in a header file.
Too bad. You can play games with macros so your single
header file compiles one way in one file and a different way
in all the others, but that's just a subterfuge.

"I accept the Universe." -- Margaret Fuller
"Gad! She'd better!" -- Thomas Carlyle

--
Er*********@sun .com

Jul 19 '06 #5
Salvatore Di Fazio wrote:
Eric Sosman wrote:
> Your program's modules can *declare* rgUnwalkableTil es[]
as often as you like, but must *define* it exactly once.

Why?
I don't must define it exactly ...
You must define it exactly once because that is how C is defined.
>The usual way of doing this is to put the declaration in a header
file that you #include in all the source modules:

/* tiles.h */
#define UNWALKABLETILES 16
extern char rgUnwalkableTil es[UNWALKABLETILES];

... and then place the definition in exactly one source file:

/* somefile.c */
#include "tiles.h"
char rgUnwalkableTil es[] = { ... as above ... };

Note that somefile.c #includes tiles.h; this is a very
good idea because it lets the compiler "see" the declaration
and the definition at the same time, which means it can detect
any inadvertent mis-matches.

But I prefere keep it in a header file.
The only way to do that is to use horrid tricks. Or use a language other
than C.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 19 '06 #6
Eric Sosman ha scritto:
>
I misspoke: You do not need to define the array at all --
provided you never use it. Section 6.9, paragraph 5:
okkey
Too bad. You can play games with macros so your single
header file compiles one way in one file and a different way
in all the others, but that's just a subterfuge.
So you suggest to me to no't put all the defines in one header but
divide these in the correct header file?

Another question, if I've something like it

-- myheader.h --
#ifndef H_MY_HEADER_
#define H_MY_HEADER_
#define A 12
#endif
-- myfirst.c --
#include <myheader.h>
....
-- mysecond.c --
#include <myheader.h>
the compiler defines twice the A?

Jul 19 '06 #7

Flash Gordon ha scritto:

The only way to do that is to use horrid tricks. Or use a language other
than C.
Thank you for your suggestion

Jul 19 '06 #8
Eric Sosman ha scritto:
I misspoke: You do not need to define the array at all --
provided you never use it. Section 6.9, paragraph 5:
okkey
Too bad. You can play games with macros so your single
header file compiles one way in one file and a different way
in all the others, but that's just a subterfuge.
So you suggest to me to no't put all the defines in one header but
divide these in the correct header file?

Another question, if I've something like it

-- myheader.h --
#ifndef H_MY_HEADER_
#define H_MY_HEADER_
#define A 12
#endif

-- myfirst.c --
#include <myheader.h>
....

-- mysecond.c --
#include <myheader.h>

the compiler defines twice the A?

I wrote this question because I don't understand why the array was
multiple-defineted in all the files that include the myheader.h

Jul 19 '06 #9
Salvatore Di Fazio schrieb:
Another question, if I've something like it

-- myheader.h --
#ifndef H_MY_HEADER_
#define H_MY_HEADER_
#define A 12
#endif

-- myfirst.c --
#include <myheader.h>
...

-- mysecond.c --
#include <myheader.h>

the compiler defines twice the A?

I wrote this question because I don't understand why the array was
multiple-defineted in all the files that include the myheader.h
Well,

1) With #define you define a preprocessor macro,
2) With

extern char rgUnwalkableTil es[UNWALKABLETILES];

you declare (means: tell the compiler that there is a rgUnwalkableTil es
and what type it hat, anywhere in any other file) the variable,

3) with

char rgUnwalkableTil es[] = { 1, 2, 3, ... };

you declare and define (means: tell the compiler, what rgUnwalkableTil es
actually is, what values it has) the variable.

You can do as many of 2) as you like in different compilation files (*.c
files) with the same name, but only one of 3) with the same name in all
compilation units that are linked together.

Macros you define with #define are another thing.

--
Thomas
Jul 19 '06 #10

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

Similar topics

2
3365
by: Chad Lupkes | last post by:
I'm just starting in php, although I have a little programming experience in other languages, but only on a surface level. I'm hoping someone can help me. I have a multidimensional array that I want to pull information from on a bunch of pages. $county = array ( array ( key=>"adams", name=>"Adams",
6
2464
by: Alexander Gausa | last post by:
hi i want to include rows in an array using include. but this always ends with an error. any idea? $files=array( array(1,2,3), array(2,3,4), include "addon" array(4,5,6) }
4
2775
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
1
6421
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
12
112090
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
11
3771
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
9
5609
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
23
7397
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
9
2107
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the size of array I need at run time without using too much memory or going over the allotted size if I choose to use this object for a different game. One idea I have is to create space * spaces = new space; then have all my accessors just convert...
6
2934
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on there position in the array. This should be possible but I can't figure out exactly how to accomplish this. The below code should better illustrate what I'm trying to do: template <int I> class Item
0
8474
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
8819
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
8597
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
7428
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...
1
6222
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
4222
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
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2809
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
1807
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.