468,168 Members | 1,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,168 developers. It's quick & easy.

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 1502
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Spin | last post: by
12 posts views Thread by reynoldscraigr | last post: by
9 posts views Thread by JKop | last post: by
6 posts views Thread by martin | last post: by
2 posts views Thread by kelly | last post: by
9 posts views Thread by Sonnich | last post: by
8 posts views Thread by Chris LaVelle | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.