-1

I am doing a project for a class and need help breaking my program into separate parts. My teacher gave us a prompt that stated which files would do what, but he didn't tell us how to write the header files. From what I learned online, I put function prototypes in from each .c files in its own .h file and included them with "header.h" in the .c files. However, I am getting compilation errors such as

course1.c:20:3: warning: implicit declaration of function ‘initialize’ [-Wimplicit-function-declaration]
   initialize(courses, subjects, CRN);
   ^
vector1.c:14:6: error: conflicting types for ‘resize’
 void resize(char ***courses, char***subjects, int **CRN) {
      ^
In file included from vector1.c:2:0:
vector.h:11:6: note: previous declaration of ‘resize’ was here
 void resize(char ***subjects, char ***courses, int **CRNs, int *size);
      ^
vector1.c:39:6: error: conflicting types for ‘deallocate’
 void deallocate(char **courses, char**subjects, int *CRN) {
      ^
In file included from vector1.c:2:0:
vector.h:12:6: note: previous declaration of ‘deallocate’ was here
 void deallocate(char **subjects, char **courses, int *CRNs, int size);

I am pretty sure my files have the right syntax because I compiled them separately into .o files and they worked fine. Can someone please state in general, how to break a program into separate files? I think I am obviously doing it wrong. One error the compiler gives me is that a function is not defined, when it is clearly defined in a header file I included.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kevin Cheng
  • 39
  • 1
  • 6
  • please format your question :) (using the **{ }** button at the top of the page after highlighting code elements) to help with readability, as at present it's quite hard to read. – jbutler483 Sep 29 '14 at 12:40
  • 1
    If you read the compilation error, you'd see there are conflicting function decelerations. `resize` and `deallocate` function is defined different in `vector.h` and `vector1.c`. `initialize` function has a warning which means use called the function before defining it. It is a warning, not a compilation error. – Halil Sep 29 '14 at 12:46

1 Answers1

1
    course1.c:20:3: warning: implicit declaration of function ‘initialize’ [-Wimplicit-       function-declaration]
   initialize(courses, subjects, CRN);

This means you need declare it as

 int initialize(courses, subjects, CRN);

or

  void initialize(courses, subjects, CRN);

In old C style if you don't specify, the compiler will treat it as returning int.

vector1.c:14:6: error: conflicting types for ‘resize’
 void resize(char ***courses, char***subjects, int **CRN) {
  ^
In file included from vector1.c:2:0:
vector.h:11:6: note: previous declaration of ‘resize’ was here
void resize(char ***subjects, char ***courses, int **CRNs, int *size);
  ^

This means you defined function resize twice in different locations and the prototype is different (they have different number of arguments). This is the same for function deallocate.

CS Pei
  • 10,869
  • 1
  • 27
  • 46