473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

stuck with file organisation and global a global array of strings

ben
hello there,

oh dear, oh dear.

here's a non global array of strings:

char *chararray[] = { "abc", "defgh", "ijklmop" };

how do i do that so chararray is global? what goes in a .h file and
then what goes in a .c file's function? i've just tried a whole load of
things but just couldn't do it.

i may have a slightly separate and related problem/misunderstanding
which may be why i can't do the above:

i have the following in a .h file:

unsigned Value;

and the following in a .c file in a function:

Value = 0;

the .h file is included from various .c files and when i compile i get

ld: multiple definitions of symbol _Value

first, "unsigned Value;" is *not* a definition right? it's a
decleration but i can't help feel that the compiler, when it says
"multiple definitions", is talking about the "unsigned Value;" part
rather than the "Value = 0;" part. i'm a bit confused about files
including headers :(

this is actually an objective-c project and when i say ".c" above i'm
lying. it's in a .m file but apparently i'm told this is totally a c
issue not objective-c.

can anyone offer me any help regarding this please?

thanks, ben.
May 16 '06 #1
8 1712
In article <16******************@x.x>, ben <x@x.x> wrote:
here's a non global array of strings: char *chararray[] = { "abc", "defgh", "ijklmop" }; how do i do that so chararray is global? what goes in a .h file and
then what goes in a .c file's function?


Look up the use of "extern".
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
May 16 '06 #2
On Tue, 16 May 2006 17:27:48 GMT, ben <x@x.x> wrote:
here's a non global array of strings:

char *chararray[] = { "abc", "defgh", "ijklmop" };

how do i do that so chararray is global? what goes in a .h file and
then what goes in a .c file's function? i've just tried a whole load of
things but just couldn't do it.
If chararray is defined at file scope, it is already 'global'
i may have a slightly separate and related problem/misunderstanding
which may be why i can't do the above:

i have the following in a .h file:

unsigned Value;

and the following in a .c file in a function:

Value = 0;

the .h file is included from various .c files and when i compile i get

ld: multiple definitions of symbol _Value

first, "unsigned Value;" is *not* a definition right?
Wrong - It is a definition, and if that header file is included more
than once Value is defined multiple times.
it's a
decleration but i can't help feel that the compiler, when it says
"multiple definitions", is talking about the "unsigned Value;" part
rather than the "Value = 0;" part. i'm a bit confused about files
including headers :(

this is actually an objective-c project and when i say ".c" above i'm
lying. it's in a .m file but apparently i'm told this is totally a c
issue not objective-c.

can anyone offer me any help regarding this please?


This is the general layout you should follow:

In one and only one C/C++/Ojective-C source file:

unsigned Value;
char *chararray[] ... ;

This will allocate memory for these objects.
(They don't need to be in the same file.)

In one and only one header file:

extern unsigned Value;
extern char *chararray[] ... ;

This says that the objects have been defined somewhere else.
(They don't need to be in the same file.)

Include the header file(s) in the C/etc. files which need access to
these objects.
May 16 '06 #3
ben wrote:

hello there,

oh dear, oh dear.

here's a non global array of strings:

char *chararray[] = { "abc", "defgh", "ijklmop" };

how do i do that so chararray is global? what goes in a .h file and
then what goes in a .c file's function? i've just tried a whole load of
things but just couldn't do it.

i may have a slightly separate and related problem/misunderstanding
which may be why i can't do the above:

i have the following in a .h file:

unsigned Value;

and the following in a .c file in a function:

Value = 0;

the .h file is included from various .c files and when i compile i get

ld: multiple definitions of symbol _Value

first, "unsigned Value;" is *not* a definition right?
It's a declaration.
It's also and a definition and a default initialization,
unless it's followed by an explicit initializing defintion.

unsigned Value;
by itself, means the exact same thing as
unsigned Value = {0};

If it was
unsigned Value;
unsigned Value = 5;
then the first line would be a declaration only
and the second line would be a definition.

But with
unsigned Value;
Value = 5;
then the first line is a definition
with a default initialization value of zero,
and the second line is an assignement statement.
it's a
decleration but i can't help feel that the compiler, when it says
"multiple definitions", is talking about the "unsigned Value;" part
rather than the "Value = 0;" part. i'm a bit confused about files
including headers :(

this is actually an objective-c project and when i say ".c" above i'm
lying. it's in a .m file but apparently i'm told this is totally a c
issue not objective-c.

can anyone offer me any help regarding this please?


Define your global in your C file like this:
unsigned Value;
Declare your global in your h file like this:
extern unsigned Value;

--
pete
May 16 '06 #4
ben
hello Roberto,

In article <cu********************************@4ax.com>, Roberto
Waltman <us****@rwaltman.net> wrote:
On Tue, 16 May 2006 17:27:48 GMT, ben <x@x.x> wrote:
here's a non global array of strings:

char *chararray[] = { "abc", "defgh", "ijklmop" };

how do i do that so chararray is global? what goes in a .h file and
then what goes in a .c file's function? i've just tried a whole load of
things but just couldn't do it.
If chararray is defined at file scope, it is already 'global'


yes but i fealt a decleration was necessary (which i guess it is from
what you say below but not how i was doing it)
and the following in a .c file in a function:

Value = 0;

the .h file is included from various .c files and when i compile i get

ld: multiple definitions of symbol _Value

first, "unsigned Value;" is *not* a definition right?


Wrong - It is a definition, and if that header file is included more
than once Value is defined multiple times.


oh, i thought "unsigned Value;" was a decleration because nothing is
being assigned to it -- but obviously that isn't the case.
This is the general layout you should follow:

In one and only one C/C++/Ojective-C source file:

unsigned Value;
char *chararray[] ... ;

This will allocate memory for these objects.
(They don't need to be in the same file.)

In one and only one header file:

extern unsigned Value;
extern char *chararray[] ... ;

This says that the objects have been defined somewhere else.
(They don't need to be in the same file.)


regarding the lines above starting with extern and in the .h files:
*they're* declerations right? so when a line like unsigned Value; is
not in a function it is a definition? and putting external infront of
it makes it not a definition but a decleration? never knew that.
unsigned Value; in a function though as is is a decleration right?

right yes your answer is very helpful, thanks. i think that's pretty
much cleared it up, i think. i haven't done/tried it yet but will do
now.

thanks very much for telling me all that.

ben.
May 16 '06 #5
ben wrote:
oh, i thought "unsigned Value;" was a decleration because nothing is
being assigned to it -- but obviously that isn't the case.
It's different when it's declared outside of a function.
This is the general layout you should follow:

In one and only one C/C++/Ojective-C source file:

unsigned Value;
char *chararray[] ... ;

This will allocate memory for these objects.
(They don't need to be in the same file.)

In one and only one header file:

extern unsigned Value;
extern char *chararray[] ... ;

This says that the objects have been defined somewhere else.
(They don't need to be in the same file.)


regarding the lines above starting with extern and in the .h files:
*they're* declerations right?


Right.
so when a line like unsigned Value; is
not in a function it is a definition?
Usually, with one exception that I can think of.
and putting external infront of
it makes it not a definition but a decleration?
"extern", yes.
never knew that.
unsigned Value; in a function though as is is a decleration right?


That's a declaration and a definition too.

--
pete
May 16 '06 #6
ben wrote:
.... snip ...
i have the following in a .h file:

unsigned Value;

and the following in a .c file in a function:

Value = 0;

the .h file is included from various .c files and when i compile i get

ld: multiple definitions of symbol _Value


Change the .h file to read:

extern unsigned Value;

and #include that in every .c file that needs to access Value,
including the .c file which defines and initializes it. Do not do
that definition inside a function, do it at the file level.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

May 16 '06 #7
ben <x@x.x> wrote:
# hello there,
#
# oh dear, oh dear.
#
# here's a non global array of strings:
#
# char *chararray[] = { "abc", "defgh", "ijklmop" };

In the .h file do
extern Type variable;
In the .c file do
Type variable = initialvalue;

The first is included by all clients and declares that variable
is Typed and it will be defined somewhere.

The second is included in one .c file and establishes in which
object module defines the variable and what its initial value is.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
God's a skeeball fanatic.
May 16 '06 #8
ben
In article <44***********@mindspring.com>, pete
<pf*****@mindspring.com> wrote:
It's a declaration.
It's also and a definition and a default initialization,
unless it's followed by an explicit initializing defintion.

unsigned Value;
by itself, means the exact same thing as
unsigned Value = {0};

If it was
unsigned Value;
unsigned Value = 5;
then the first line would be a declaration only
and the second line would be a definition.

But with
unsigned Value;
Value = 5;
then the first line is a definition
with a default initialization value of zero,
and the second line is an assignement statement.


excellent -- thanks for your replies pete, and thanks to all the other
replyers also. i have it working now of course and it's a bit clearer
to me.

cheers, ben.
May 17 '06 #9

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

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
2
by: Spin | last post by:
Let me admit right up front this is my first foray into php. I'm trying to simply use a small data file to populate a page. I'm using the file() function but I want my "data records" to be...
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
9
by: JKop | last post by:
Let's say you have a global const variable for the name of your application. Which do you think is preferrable?: A) char const g_application_name = "ChocolateCheese"; B) const char* const...
6
by: martin | last post by:
Hi, I have noticed that every aspx page that I created (and ascx file) has an assosiated resource file aspx.resx. However what I would like to do is have a single global resource file for the...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
9
by: Sonnich | last post by:
Hi! I want to have a number of strings in another file, which I can include everywhere... but I cannot make them reachable from the top file. I have looked at var and global to to this but...
8
by: Chris LaVelle | last post by:
I don't know the easiest way to explain this so, I'm going to give an example of how it is today....and what I'm trying to accomplish. in the header... typedef struct XXX_ELEMENT_tag { UINT8 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.