Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stuck with file organisation and global a global array of strings

Reply
Thread Tools

stuck with file organisation and global a global array of strings

 
 
ben
Guest
Posts: n/a
 
      05-16-2006
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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-16-2006
In article <160520061825354251%>, ben <> 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
 
Reply With Quote
 
 
 
 
Roberto Waltman
Guest
Posts: n/a
 
      05-16-2006
On Tue, 16 May 2006 17:27:48 GMT, ben <> 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-16-2006
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
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      05-16-2006
hello Roberto,

In article <>, Roberto
Waltman <> wrote:

> On Tue, 16 May 2006 17:27:48 GMT, ben <> 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-16-2006
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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-16-2006
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/>

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      05-16-2006
ben <> 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.
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      05-17-2006
In article <>, pete
<> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Organisation of python classes and their methods Paul Rubin Python 10 11-03-2012 01:38 AM
Re: Organisation of python classes and their methods Peter Otten Python 0 11-02-2012 08:00 AM
Thoughts on file organisation David Mearsen C Programming 65 01-31-2008 02:24 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
file organisation -[Myth]- NZ Computing 4 02-01-2004 06:19 AM



Advertisments