Thread: Extern pointers

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    Extern pointers

    Am i doing this right. This is what i have got.

    Main.c
    Code:
    for(i=0; i<10; i++)
        {
            printf("Data 1 - %d\n", NODE[i].data1);
            printf("Data 2 - %d", NODE[i].data2);
            printf("\n-----------\n");
        }
    Lib.c
    Code:
     NODE = malloc( sizeof(struct node) * 10 );
    Struct.h
    Code:
    struct node
    {
        int data1;
        int data2;
    };
    extern struct node *NODE;
    I get undefined reference on main.c and perhaps in Lib.c. Does extern works for pointers?

    ssharish

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that should work just fine [at least as long as "Lib.c" isn't a DLL].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    AFAIK, if you're just using the include directives, there's no need to do extern.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    AFAIK, if you're just using the include directives, there's no need to do extern.
    The compiler itself doesn't know if the "source" is part of an include file or not. For function declarations, "extern" is not important, but for variables you do need an "extern" to tell the compiler NOT to generate the space for the variable, but let that be resolved at the linker stage.

    @ssharish: You do need a variable definition too, something like
    Code:
    struct node *NODE;
    This should go in either of main or lib.c [or in a "globals.c" where you would have ALL of the global variables.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Hey Map, that worked. Took out the extern keyword in my .h file and included extern on my .c file. Everything worked fine.

    Do basically i was misplacing things incorrectly.

    ssharish

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Eh? But it's safe to use extern in headers.
    You just need to make sure there's an actual definition somewhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You want extern in your headers, and the source in your .c files, because the headers might be included more than once.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The thing is, you can have any number of extern declarations of a variable before you declare the variable itself. This compiles and runs with no worries:
    Code:
    #include <stdio.h>
    
    extern int x;
    extern int x;
    extern int x;
    int x;
    
    int main() {
        x = 3;
        printf("&#37;i\n", x);
        return 0;
    }
    So you can have a header file like this
    Code:
    /* header.h */
    extern int x;
    because it works in code that doesn't declare the variable
    Code:
    #include <stdio.h>
    #include "header.h"
    
    void print(void) {
        printf("%i\n", x);
    }
    as well as in code that does:
    Code:
    #include "header.h"
    
    int x;
    
    int main() {
        x = 3;
        print();
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just to summarize: You should NOT remove extern in your header-file, you should have ONE .c file that contains a "without extern" definition of the variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Wow, thanks a lot. That makes things clear. I always had this confusion on how to use extern variable. Perhaps whenever I use them something goes wrong somewhere lol. I think I am on right track now. Just to make things clear. Here is what I have learnt from this thread

    The extern variables are supposed to be declared in the header files. But to reference those extern variable in the .c file, the variable have to be redeclared again in the .c file some where.

    @DWK - The first sample code which you posted made me to think a bit. But I couldn't answer my question by myself. Shouldn’t there be a clash if you have a variable with the same name multiple times. But in your case it tents to compile fine. Which mean that its been declared just once in the symbol table. How is that?

    Thanks once again for all of you, for just a great help. You made a person happy

    ssharish

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    An extern declaration does not create a new symbol; it just says "FYI, there is an int x somewhere; we'll find it later." This way the compiler knows the type of all the symbols, and can generate most of the right code; the linker's job is then to match this mystery variable with its actual declaration in a file.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    The extern keyword is just used to tell the compiler that there is some variable by a name of that type. It does not actually allocate space for it, so you can have as many statements of extern as you want, as long as there is a corresponding non-extern statement in one file. That's why header files don't get multiple definition errors, because even though they are included in more that one source file, they are externs, so they don't actully allocate space. They just inform the compiler of the type and the name. The compiler then encodes this information into the object file created form each source file, and then the linker checks all of those references to the same name and type and "links" them together so that they refer to the same variable. However, if the linker finds out that there is more than one non-extern statement that has the same name, then that's when the linker gives a multiple definition error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  2. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM
  5. extern keyword and structures
    By GuitGentlyWeeps in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 07:02 AM