extern struct

This is a discussion on extern struct within the C Programming forums, part of the General Programming Boards category; Hi, I'm declaring my structs in a seperate .h file like this Code: struct { ... } eQ1; I've included ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    extern struct

    Hi,

    I'm declaring my structs in a seperate .h file like this

    Code:
    struct {
    
    	...
    
    } eQ1;
    I've included the .h file in main so accessing them from main.c is fine. However if I then include this in other files I get errors (redeclaration). Do I need to use extern in the other files when referencing my eQ1 struct?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    You need to separate the declaration of your struct from the declaration of a variable of that struct type. As written, your struct has no name, so it is impossible to refer to it.

    In the .h
    Code:
    struct foo {
      int member;
    };
    extern struct foo eQ1;
    In ONE .c file only
    Code:
    #include "myheader.h"
    struct foo eQ1;
    In all other .c files which use it
    Code:
    #include "myheader.h"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thanks everybody, I've done as you've suggested but I'm still having issues. Now I'm getting 'redeclaration' errors, as a result of include the same header file more than once in my project. I think it's because I haven't set up my project correctly. What I have is

    main.c
    a.h
    b.h
    c.h

    In a, b and c header files I'm actually doing some work. I've noticed that other projects will have something like this

    main.c
    a.h
    a.c
    b.h
    b.c
    c.h
    c.c

    Can someone please explain why this is done this way? As you can see by my username, I am a beginner. But I am trying to find out on my own too.

    Thanks

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    put
    Code:
    #ifndef FILE__H
    #define FILE__H
    /* code */
    #endif
    in your header files

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    What does this do/achieve?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    if you include the same file twice, it won't redeclare the variables.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Is this a 'best practice' method? I didn't find much info on it on google.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    It's what almost everyone does.. Except for compilers supporting the "#pragma once" command.

  10. #10

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21