473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Defining struct in headers

22
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "bitwise.h"
  6. #include "helpers.h"
  7. #include "phone.h"
  8.  
  9. int main() {
  10.     struct phone * phone = malloc(sizeof(struct phone));
  11.     ...
  12.     ...
  13. }
phone.c
Expand|Select|Wrap|Line Numbers
  1. #include "phone.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. struct message {
  6.     char * part1, * part2, * part3;
  7.     unsigned int msgID;
  8.     char created[9];
  9.     unsigned int phoneNum;
  10.     unsigned int size;
  11.     unsigned char flags;
  12. };
  13.  
  14. struct phone {
  15.     struct message msg[200];
  16.     char text[600][64];
  17.     unsigned char bitmap[75];
  18.     int count;
  19. };
  20.  
  21. struct message * newMessage(struct phone * phone, char * params[]) {
  22.     return NULL;
  23. }
  24.  
  25. #ifdef SOLO
  26.  
  27. int main() { return 0; }
  28.  
  29. #endif
phone.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef PHONE_H_
  2. #define PHONE_H_
  3.  
  4.  
  5.  
  6. #endif /*PHONE_H_*/
  7.  
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
Oct 12 '08 #1
2 2643
boxfish
469 Expert 256MB
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.
Oct 12 '08 #2
krreks
22
Works like a charm :) Thanks!
Oct 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
3
by: paul.wilkins | last post by:
Hi, I have a function I have written that works with g++ 2.95.3 that I would like to use with newer g++ compilers >= 3.2.0. The function is a stream manipulator similar to setw(). The code...
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
16
by: Raj Kotaru | last post by:
Hello all, I recently came across the following segment of code that defines a C struct: typedef struct { unsigned char unused_bits:4; unsigned char wchair_state:2; } xyz;
13
by: Steffen Loringer | last post by:
Hi all, I can't figure out why a can't use my struct TestStructure in other files. Any ideas? My project consists of main.c,lib1.h,lib1.c,lib2.c,lib2.h . main.c...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
23
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
5
by: chrisstankevitz | last post by:
Why does this code only compile if GLOBAL_IN_STRUCT is defined? It creates a templated class C<T> and defines a global operator* that takes a C<T> on the LHS and a T on the RHS. In the...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.