Thread: extern and typedef

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    extern and typedef

    Ok, say I have a declaration.h file which inside of it has the following:

    extern int lineno;

    typedef struct treeNode
    {
    int lineno;
    some other stuffs here
    }


    and I have another file which is test.c, which wants to use the lineno variable on that declaration.h file. I already include declaration.h on top of the header of test.c, however it still can't find lineno. It gives me an error of:

    undefined reference to `lineno'


    So what should I do so that I can use the lineno variable? Thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You need to actually define

    Code:
    int lineno;
    Somewhere in a C file. And inside a structure doesn't cut it. That is a scoped context.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    but the cuts out the main purpose of what I really want, what I want to do is that so that I can reference lineno with whatever is declared for lineno inside the struct it self...

    basically so that I can use lineno in any .c file that I want
    Last edited by -EquinoX-; 10-17-2008 at 01:56 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Right, which isn't something that you are robbed of doing by following the proceedure I just gave you... On a random side note, when did the tabs on my sig get all jacked up?

    Puts the global in ONE of your C files.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    if I do :

    int lineno in one of my .c file then will the struct that has a lineno inside of it has the same value as lineno in a .c file?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course not. Each object of that struct will have its own lineno variable (which would have to be accessed via object.lineno or objectptr->lineno).

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    But what you could or maybe even should do is initialize every struct to have a copy of lineno into it.

    Or if they need a synchronous copy of lineno you can simply change your design a bit.

    Example:
    Code:
    struct treeNode 
    { 
      int *lineno;
      ...
    };
    And have every struct treeNode->lineno = &lineno;

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    so is int *lineno a pointer which is included in all the struc variable and then is immediately used when the struc is initialized?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it's not.
    You should stop trying to make the variable inside the struct refer to the global one.
    Essentially, what you want to do is initialize members in the struct before use.
    So, I have two suggestions:
    1) If you can use C++, do it. Constructors will do exactly what you want there.
    2) If you still intend to use C, create a function that creates and/or initializes the struct in the way you want. Then call it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-04-2009, 02:03 PM
  2. need help with extern pointers.
    By broli86 in forum C Programming
    Replies: 17
    Last Post: 04-11-2008, 09:16 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  5. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM