Thread: struct declarations in header files

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    50

    struct declarations in header files

    I am currently experiencing the error:

    file1.c:51: error: storage size of 'var' isn't known

    I am thinking that it is because I've defined a struct in a .c file, and it has no corresponding declaration in its header file. I've got file1.c, which is the program I am trying to declare a struct variable in (on line 51, as indicated by the error above), file2.c, which contains the struct definition, and file2.h which contains declarations for other functions in file2.c. Line 51 in file1.c looks like this:

    Code:
    struct my_struct var;
    So, I understand that file1.c really has no way of knowing what my_struct is, because it only includes a statement #include "file2.h". It seems like I'm missing a struct declaration in file2.h. How do I do this? By the way, the structure definition in file2.c looks like this:

    Code:
    struct my_struct {
       char first_name[20];
       char last_name[20];
    };

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: struct declarations in header files

    You can include the c file to another C file.
    Ex:
    #include "file2.c"

    But you should not use main function in that file2.c. If you have main function in file2.c while including this it will give error.

    Otherwise you can maintain the common header file for that programs. In that header file you can define the structure.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    50
    Would you mind providing the structure definition for the header file in this example? Thanks, much appreciated!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nicoeschpiko
    So, I understand that file1.c really has no way of knowing what my_struct is, because it only includes a statement #include "file2.h". It seems like I'm missing a struct declaration in file2.h. How do I do this? By the way, the structure definition in file2.c looks like this:
    You actually need the struct definition to be visible (as in a declaration would more generally refer to things like a forward declaration of the struct, which is sufficient to declare a pointer to the struct type).

    Move the struct definition from file2.c to file2.h, then #include "file2.h" in file2.c.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    I think your suspection is right ,
    You must be defined that struct in a header file , then you can include in the normal .c file.
    in that C file you can create a structure variable

    Code:
     
    Filename : struct.h 
    
     struct my_struct {
               char first_name[20];
                  char last_name[20];
    };
    
    Filename : s1.c 
    #include<stdio.h> 
    #include "struct.h"
    
    main()
    {
    
    struct my_struct var1 ;  // Works perfectly 
    
    }
    No matter how many files you are using , you should simply include
    the header file.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    50

    error: incompatible types in assignment

    Now I am having the following issue:

    error: incompatible types in assignment

    my file1.c looks like this:

    Code:
    #include <stdio.h>
    #include "file2.h"
    
    int main() {
    	struct my_struct new_struct;
    	new_struct = create_my_struct();
    	printf("%s %s", new_struct.first_name, new_struct.last_name);
    	return 0;
    }

    and, my file2.c looks like this

    Code:
    #include <string.h>
    
    struct my_struct create_my_struct() {
    	struct my_struct ms;
    
    	strcpy(ms.first_name, "john");
    	strcpy(ms.last_name, "smith");
    
    	return ms;
    };

    The issue relates to line6 in file1.c (new_struct = create_my_struct();). These both seem to be of the same type (file1 declares a variable of type my_struct, and file2 returns a variable of type my_struct). Any idea what the issue might be?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably forgot to declare your create_my_struct() function in the header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    50
    You're right! Thanks again! Alright, too many ridiculous mistakes...time for sleep.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM