Thread: multiple file scope question

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    multiple file scope question

    I am making a program with multiple *.c files and multiple *.h files.
    I want to declare a "typedef struct" like this:

    Code:
    typedef struct node{
        char data;
        struct node* next;
    )NODE;
    Where do I have to declare it in order for it to be recognized throughout the whole program?

    Do I have to make it external?
    thanks.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    One way is to put it in an include file that is included in every module, with something like this:
    Code:
    #ifndef NODE_DEF
    #define NODE_DEF 
     typedef struct node
     {
         char data;
         struct node* next;
     ) NODE;
    #endif

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Do I have to make it external?
    The extern keyword is used to tell one file that the actual declaration of that variable/struct/whatever is in another file.

    For example, if you have a global variable in one .c file declared as int ImAGlobalVariable;, then you could put extern int ImAGlobalVariable; in a header file that you include in other .c files.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    thanx for the replies,
    i was playing around with it a little more,
    whould it work if i declared the struct in one of the .h files and include it in main.c,
    whould all the files recognize it that way?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's fine to define a struct in a header file, but you want to declare instances of the struct in .c files. So you might have this:
    Code:
    // file.c
    
    struct somestruct foo;
    Code:
    // file.h
    #ifndef FILE_C_
    #define FILE_C_
    
    struct somestruct
    {
      int a;
      int b;
    };
    
    extern struct somestruct foo;
    #endif
    And then any .c file in which you include file.h can use struct somestruct and will also have access to the foo instance of that struct.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    I'd been meaning to ask this exact question(well, I thought that's how it worked, but I wanted to make sure it was okay).
    I live in a giant bucket.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM