Thread: Struct Problem

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Struct Problem

    I'm having a problem with a structure. I declare a structure in the file "Config.c" like this
    Code:
    struct setting{
           int ScreenWidth;
           int ScreenHeight;
    };
    struct setting *Settings;
    Then I use this function to set values for ScreenHeight and ScreenWidth
    Code:
    InitSettings(){
         Settings = calloc(1,sizeof(struct setting));
         Settings->ScreenWidth = 1024;
         Settings->ScreenHeight = 768;
    }
    Then I want to accses the struct from another file.
    Code:
    extern struct setting *Settings;
    
    InitGUI(){
         int GameWindowLeft;
         GameWindowLeft = Settings->ScreenWidth - 320;
    }
    The problem is that I get this error message "dereferencing pointer to incomplete type "

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is the structure definition visible in the file that produces the error? Is this part in a header?
    Code:
    struct setting{
           int ScreenWidth;
           int ScreenHeight;
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    No the definition is in the file "config.h" and the file that produces the error is "Video.c".

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Did you include "config.h" in "Video.c"? I'm guessing you have to do this because an extern isn't enough, since you're accessing one of the data members.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    It works!!! I didn't have a clue that you had to include the header file when accsessing an external structure. I will remember that, thank you!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You have to tell the compiler what a user-defined data type "looks" like so that it "knows" whether you are using it correctly.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM