extern local variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • j0mbolar

    extern local variables

    void myfunction(void )
    {
    extern int myvariable;
    return ;
    }

    what is the point in allowing local variables to have external linkage?
    its scope is only myfunction so it can't be used anywhere else.
  • Dan Pop

    #2
    Re: extern local variables

    In <2d31a9f9.04030 40513.5f9550b7@ posting.google. com> j0mbolar@engine er.com (j0mbolar) writes:
    [color=blue]
    >void myfunction(void )
    >{
    > extern int myvariable;
    > return ;
    >}
    >
    >what is the point in allowing local variables to have external linkage?[/color]

    You're confused.

    extern int myvariable;

    does NOT *define* myvariable, it merely *declares* myvariable as an
    object defined somewhere else with external linkage.
    [color=blue]
    >its scope is only myfunction so it can't be used anywhere else.[/color]

    The scope of the myvariable *identifier* is only myfunction. The actual
    object was *defined* elsewhere and it's still available to any function
    *declaring* it.

    Example:

    fangorn:~/tmp 264> cat test.c
    #include <stdio.h>

    void foo(void)
    {
    extern int myvariable;
    printf("%d\n", myvariable);
    }

    void foo2(void)
    {
    extern int myvariable;
    printf("%d\n", myvariable * 2);
    }

    int main()
    {
    foo();
    foo2();
    return 0;
    }

    int myvariable = 42;

    fangorn:~/tmp 265> gcc -ansi -pedantic test.c
    fangorn:~/tmp 266> ./a.out
    42
    84

    You can also move the definition of myvariable to another source file.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email: Dan.Pop@ifh.de

    Comment

    • Leor Zolman

      #3
      Re: extern local variables

      On 4 Mar 2004 05:13:31 -0800, j0mbolar@engine er.com (j0mbolar) wrote:
      [color=blue]
      >void myfunction(void )
      >{
      > extern int myvariable;
      > return ;
      >}
      >
      >what is the point in allowing local variables to have external linkage?
      >its scope is only myfunction so it can't be used anywhere else.[/color]

      The /name/ may have local scope, but the variable it refers to most
      certainly will not. This would be something you might choose to do
      (personally, I don't think I've ever written a declaration like this) to
      bring a file-scope variable defined in, say, other another translation unit
      into scope within the body of this function, in case it isn't in scope
      already. (Another possibility is that it is brought into file scope /below/
      the point where this function is defined.)

      Of course, if myvariable is already declared at file scope at the point
      where your function definition resides, this would effectively constitute a
      no-op declaration.
      -leor



      Leor Zolman
      BD Software
      leor@bdsoft.com
      www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
      C++ users: Download BD Software's free STL Error Message
      Decryptor at www.bdsoft.com/tools/stlfilt.html

      Comment

      • Derk Gwen

        #4
        Re: extern local variables

        j0mbolar@engine er.com (j0mbolar) wrote:
        # void myfunction(void )
        # {
        # extern int myvariable;
        # return ;
        # }
        #
        # what is the point in allowing local variables to have external linkage?
        # its scope is only myfunction so it can't be used anywhere else.

        What's the point in disallowing it? It's statically allocated and visible
        to all other declarations of the same extern name.

        --
        Derk Gwen http://derkgwen.250free.com/html/index.html
        OOOOOOOOOO! NAVY SEALS!

        Comment

        Working...