Alex
Alex

Reputation: 2076

C program not compiling when using headers

(C PROGRAM) I'm trying to compile a main.c that uses a header but I'm getting the errors below. When I don't use the header (all methods in the main file) everything works.

In a string S, the program find all words occurrences and returns the word that appears the most.

I'm compiling using: gcc main.c

Thank you.

errors

In file included from main.c:9:0:
frequence.h:4:16: warning: useless storage class specifier in empty declaration [enabled by default]
main.c: In function ‘main’:
main.c:15:10: error: variable ‘word’ has initializer but incomplete type
main.c:15:10: warning: passing argument 1 of ‘show_all_words’ from incompatible pointer type [enabled by default]
frequence.h:6:17: note: expected ‘char *’ but argument is of type ‘char (*)[34]’
main.c:15:10: error: invalid use of undefined type ‘struct stat_mot’
main.c:15:19: error: storage size of ‘word’ isn’t known

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "frequence.h"

#define LONGUEURMAX 4

int main(char *argv[]) {
  char texte[] = ";! one two, tree foor one two !:;";
  struct stat_mot word = show_all_words(&texte);

  printf("%s : %d\n", word.word, word.frequency);

  return (EXIT_SUCCESS);
};

frequence.h

#ifndef DEF_FREQUENCE
#define DEF_FREQUENCE

typedef struct stat_mot;
int count_word(char * , char * );
struct stat_mot show_all_words(char *);

#endif

frequence.c

#include "frequence.h"

typedef struct stat_mot {
    char * word;
    int frequency;
} stat_mot;

int count_word(char * mot, char * text) {
  int n = 0;
  char *p;

  p = strstr(text, mot);  
  while (p != NULL) {
    n++;
    p = strstr(p + 1, mot);
  }  

  return n;
}

stat_mot show_all_words(char * text) {
    char * text_rw = strdup(text);
    char * p = strtok(text_rw, " .,;:-!?");

    char word_to_return[strlen(text)];
    int  word_frequence = 0;

    while (p != NULL) {
      if (strlen(p) >= 0) {
        int offset = p - text;
        int count = count_word(p, text);

        if (word_frequence < count) {
          strcpy(word_to_return, p);          
          word_frequence = count;
        }
      };

      p = strtok(NULL, " .,;:-!?");
    }

    free(text_rw);
    struct stat_mot word = { word_to_return, word_frequence };
    return word;
}

Upvotes: 1

Views: 1183

Answers (8)

Rugal
Rugal

Reputation: 2717

May be you should write this

typedef struct stat_mot stat_mot;

in your frequence.h and then

typedef struct stat_mot {
    char * word;
    int frequency;
};

in .c file.

.h file is just a declare of data file or method,but you should declare it clearly.

TIPs: there is no need a ; at the end of main(){};

Upvotes: 0

LSerni
LSerni

Reputation: 57378

You must put your stat_mot structure in frequence.h.

Additionally, main() should be declared like

int main(int argc, char **argv)

and texte should be a char *:

int main(char *argv[])
{
    char *texte = ";! one two, tree foor one two !:;";
    struct stat_mot word = show_all_words(texte);

You also ought to include <string.h> and <stdlib.h> in frequence.c since you use strstr, strlen and free.

In frequence.c, strlen is always greater or equal to zero, so the comparison will always be true.

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279215

At the least, you need to move the definition of stat_mot from frequence.c to frequence.h. main() tries to create an instance of it, you can't do that without the struct definition.

There are some other issues too:

  • I find it hard to believe that the type mismatch between char* and char(*)[34] is resolved by putting everything into one file. So I expect the fix has nothing to do with your organization of the code into files, it's just that you should pass texte, not &texte.
  • "useless storage class specifier in empty declaration" -- is a confusing error message, but it comes about because in the C syntax typedef is a storage class specifier. Don't ask why. Basically, typedef struct stat_mot; is wrong, but that's OK because you can remove it anyway when you move the struct definition.

Upvotes: 5

tozka
tozka

Reputation: 3451

struct stat_mot needs to be defined in the header file or use pointers.

Oh and (though this is not the problem)

typedef struct stat_mot;

you need to specify second type name like this:

typedef struct stat_mot stat_mot;

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84151

Just put the structure declaration into the header file:

/* in frequence.h */
struct stat_mot {
    char* word;
    int frequency;
};

so both translation units (i.e. the .c files) see it.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

You declare the structure stat_mot in the header file (a forward declaration) but you define it in the source file frequence.c. Therefore it's not usable from main.c.

Upvotes: 1

penelope
penelope

Reputation: 8419

The header is only good for the definitions -- e.g. compiling actually works, but linker does not know where to look for the bodies of the functions.

Try compiling with gcc main.c frequence.c

Also, typedef struct stat_mot; does not have much meaning -- struct stat_mot is not defined at this moment, and also, you are not giving it a new name with this typedef. I think separating the struct definition does not make sense even if it was allowed - after all, if you just distribute the header file, people should know how to use the struct (e.g. see what it contains)

Upvotes: 1

Hauleth
Hauleth

Reputation: 23556

Your struct definition must be in header.

Upvotes: 3

Related Questions