Re: extern typedef struct

Discussion in 'C Programming' started by Seebs, May 18, 2010.

  1. Seebs

    Seebs Guest

    On 2010-05-18, Jeff <> wrote:
    > object obj = new_object();


    This variable can never be seen outside main(), because it was
    declared inside a block.

    > sock.c ===============
    >
    > sock
    > new_socket()
    > {
    > int thing = object_get_something(obj); /* compiles but doesn't link
    > */
    > }


    Obviously, sock.c must include setup.h. :)

    Anyway, the problem here is that the "obj" variable is declared inside main(),
    and an extern ... declaration won't find a local variable, only a variable
    declared outside any functions and with external linkage. ("External linkage"
    is a fancy way of saying "not declared static"; it's the default behavior.)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, May 18, 2010
    #1
    1. Advertising

  2. Seebs

    Seebs Guest

    On 2010-05-18, Jeff <> wrote:
    > Well, I'd like to apologize for wasting everyone's time (including my
    > own)


    Yes, how dare you ask a question about C to which the answer is topical!

    I don't think it's a "waste" of time to talk about C here. People make
    mistakes, whether or not they're very experienced, and even if you think
    it should have been "obvious" to you, well, this is one more chance for
    someone who doesn't know that much about C yet to find it when searching.
    :)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, May 18, 2010
    #2
    1. Advertising

  3. Seebs

    Chad Guest

    On May 18, 6:46 am, Seebs <> wrote:
    > On 2010-05-18, Jeff <> wrote:
    >
    > >   object obj = new_object();

    >
    > This variable can never be seen outside main(), because it was
    > declared inside a block.
    >
    > > sock.c ===============

    >
    > > sock
    > > new_socket()
    > > {
    > >   int thing = object_get_something(obj); /* compiles but doesn't link
    > > */
    > > }

    >
    > Obviously, sock.c must include setup.h.  :)
    >
    > Anyway, the problem here is that the "obj" variable is declared inside main(),
    > and an extern ... declaration won't find a local variable, only a variable
    > declared outside any functions and with external linkage.  ("External linkage"
    > is a fancy way of saying "not declared static"; it's the default behavior..)
    >


    So where would you place 'object obj = new_object();'? Inside
    new_socket()?
     
    Chad, May 18, 2010
    #3
  4. Chad <> wrote:
    > On May 18, 6:46 am, Seebs <> wrote:
    > > On 2010-05-18, Jeff <> wrote:
    > >
    > > >   object obj = new_object();

    > >
    > > This variable can never be seen outside main(), because it was
    > > declared inside a block.
    > >
    > > > sock.c ===============

    > >
    > > > sock
    > > > new_socket()
    > > > {
    > > >   int thing = object_get_something(obj); /* compiles but doesn't link
    > > > */
    > > > }

    > >
    > > Obviously, sock.c must include setup.h.  :)
    > >
    > > Anyway, the problem here is that the "obj" variable is declared inside main(),
    > > and an extern ... declaration won't find a local variable, only a variable
    > > declared outside any functions and with external linkage.  ("External linkage"
    > > is a fancy way of saying "not declared static"; it's the default behavior.)
    > >


    > So where would you place 'object obj = new_object();'? Inside
    > new_socket()?


    If 'obj' is meant to be a global variable it can be defined in
    any of the source files (as long as it's in just one of them).
    For that it may not be within a function (otherwise it wouldn't
    be global). But then a line like

    object obj = new_object();

    can't be used since the initialization value for a global variable
    must be a compile time constant - and a call to a function can only
    be done at run time. So you put the definition of the variable with-
    out that initialization outside of all functions in one of the source
    files and do the initialization e.g. in main() or just before 'obj'
    is used for the first time (you could do the initialization anytime
    between the start of main() and the first use of obj' but doing it
    either at the very start or when it's needed would look most natural
    to me, but YMMV...)
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 18, 2010
    #4
    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. DanielEKFA
    Replies:
    8
    Views:
    615
    DanielEKFA
    May 16, 2005
  2. Steven T. Hatton
    Replies:
    2
    Views:
    681
  3. Dirk Bruere at NeoPax

    How to declare extern typedef struct?

    Dirk Bruere at NeoPax, Dec 6, 2008, in forum: C Programming
    Replies:
    4
    Views:
    11,683
    Dirk Bruere at NeoPax
    Dec 7, 2008
  4. Jens Thoms Toerring

    Re: extern typedef struct

    Jens Thoms Toerring, May 18, 2010, in forum: C Programming
    Replies:
    0
    Views:
    637
    Jens Thoms Toerring
    May 18, 2010
  5. BruceS

    Re: extern typedef struct

    BruceS, May 18, 2010, in forum: C Programming
    Replies:
    6
    Views:
    1,037
    BruceS
    May 19, 2010
Loading...

Share This Page