user2559503
user2559503

Reputation: 289

C error: storage size isn't known

I'm trying to create a struct to be used in a Linked List that looks like this:

#ifndef MYGREP_H
#define MYGREP_H

typedef struct occurrenceType Occurrence;

struct occurrenceType {
    char* line;
    int lineNumber;
    int wordNumber;
    Occurrence *next;
};

#endif

but when I try to allocate memory using sizeof(Occurrence) I get the error "Invalid application of 'sizeof' to incomplete type 'Occurrence.' I've tried several different structure declaration formats with no luck. Can someone tell me what I'm doing wrong? Thanks!

Upvotes: 0

Views: 3797

Answers (2)

asifaftab87
asifaftab87

Reputation: 1443

Struct is user defined data type in c. Before the declaration of occurrenceType you are trying to use it and hence before its declaration or definition if you try to use it then it is an error. Your code should be

  #ifndef MYGREP_H
  #define MYGREP_H
  struct occurrenceType {
  char* line;
  int lineNumber;
  int wordNumber;
  Occurrence *next;
  };
  typedef struct occurrenceType Occurrence;
  #endif

First declaration then use it. Another it may be some spell mismatch so try to use this

Upvotes: 0

razeh
razeh

Reputation: 2765

Your first struct typedef declaration:

                   v
typedef struct occurenceType Occurrence;
                   ^

has one 'r' on "occurencyType" but your definition:

           vv
struct occurrenceType {
           ^^ 
    char* line;
    int lineNumber;
    int wordNumber;
    Occurrence *next;
};

has two 'r's.

Upvotes: 2

Related Questions