stuck with file organisation and global a global array of strings

Discussion in 'C Programming' started by ben, May 16, 2006.

  1. ben

    ben Guest

    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.
    ben, May 16, 2006
    #1
    1. Advertising

  2. 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
    Walter Roberson, May 16, 2006
    #2
    1. Advertising

  3. 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.
    Roberto Waltman, May 16, 2006
    #3
  4. ben

    pete Guest

    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
    pete, May 16, 2006
    #4
  5. ben

    ben Guest

    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.
    ben, May 16, 2006
    #5
  6. ben

    pete Guest

    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
    pete, May 16, 2006
    #6
  7. ben

    CBFalconer Guest

    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/>
    CBFalconer, May 16, 2006
    #7
  8. ben

    SM Ryan Guest

    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.
    SM Ryan, May 16, 2006
    #8
  9. ben

    ben Guest

    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.
    ben, May 17, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  2. David Mearsen

    Thoughts on file organisation

    David Mearsen, Jan 27, 2008, in forum: C Programming
    Replies:
    65
    Views:
    1,356
    santosh
    Jan 31, 2008
  3. Paul Rubin
    Replies:
    10
    Views:
    257
    Steven D'Aprano
    Nov 3, 2012
  4. Peter Otten
    Replies:
    0
    Views:
    172
    Peter Otten
    Nov 2, 2012
  5. Robert Kern
    Replies:
    0
    Views:
    211
    Robert Kern
    Nov 2, 2012
Loading...

Share This Page