Defining struct in headers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krreks
    New Member
    • Oct 2008
    • 22

    #1

    Defining struct in headers

    I'm experiencing some (beginner) problems with my header files...

    I'm writing an message application and want to split my application into multiple files. So far, everything related to bitwise operations is in one, another one contains various other handy functions.

    Now I want to make a file with the phone and message related functions. In this file I would like to define the structs as well, but I'm facing the following error:

    error: invalid application of 'sizeof' to incomplete type 'struct phone'
    This is only when the struct is moved outside the main file.. The file is include like this:

    top of main file

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    #include "bitwise.h"
    #include "helpers.h"
    #include "phone.h"
    
    int main() {
    	struct phone * phone = malloc(sizeof(struct phone));
    	...
    	...
    }
    phone.c
    Code:
    #include "phone.h"
    #include <stdlib.h>
    #include <stdio.h>
    
    struct message {
    	char * part1, * part2, * part3;
    	unsigned int msgID;
    	char created[9];
    	unsigned int phoneNum;
    	unsigned int size;
    	unsigned char flags;
    };
    
    struct phone {
    	struct message msg[200];
    	char text[600][64];
    	unsigned char bitmap[75];
    	int count;
    };
    
    struct message * newMessage(struct phone * phone, char * params[]) {
    	return NULL;
    }
    
    #ifdef SOLO
    
    int main() { return 0; }
    
    #endif
    phone.h
    Code:
    #ifndef PHONE_H_
    #define PHONE_H_
    
    
    
    #endif /*PHONE_H_*/
    I guess there is a trivial solution to this error, but after consulting the C-faq I thought it might be just as good answering here :)

    K
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Try taking the structs out of phone.c and putting them in phone.h. Structs and function declarations go in header files; function definitions go in the corresponding source files.

    Comment

    • krreks
      New Member
      • Oct 2008
      • 22

      #3
      Works like a charm :) Thanks!

      Comment

      Working...