stuck with file organisation and global a global array of strings

B

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.
 
R

Roberto Waltman

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.
 
P

pete

ben said:
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;
 
B

ben

hello Roberto,

Roberto said:
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)
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.
 
P

pete

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.
 
C

CBFalconer

ben said:
.... 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/>
 
S

SM Ryan

# 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.
 
B

ben

pete said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top