C error: storage size isn't known - c

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!

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.

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

Related

C requires that a struct or union has at least one member - but members *have* been defined

I have a problem with my struct definition.
I define it here:
#ifndef CHALLENGE_SYSTEM_H_
#define CHALLENGE_SYSTEM_H_
#include "visitor_room.h"
typedef struct SChallengeRoomSystem
{
#include "challenge_room_system_fields.h";
} ChallengeRoomSystem;
#endif
And the file "challenge_room_system_fields.h" is:
#ifndef CHALLENGE_ROOM_SYSTEM_FIELDS_H_
#define CHALLENGE_ROOM_SYSTEM_FIELDS_H_
#include "visitor_room.h"
struct VisitorList {
Visitor* visitor;
struct VisitorList* next;
};
struct VisitorList* list;
char *name;
int num_of_challenges;
Challenge* challenges;
int num_of_rooms;
ChallengeRoom* rooms;
int last_time;
#endif
The following code gives me the error: "error C2016 C requires that a struct or union has at least one member". I don't understand why it doesn't work because I did declare members in the struct.
P.S. It's part of my homework and I have to define the members of the struct in the "challenge_room_system_fields.h" file. Also, "visitor_room.h" is another file in this project but it's not the reason of the problem for sure.

error: expected specifier-qualifier-list before when creating a struct array

I'm learning C language and I have a problem.
I have a struct and I initialize it with {0}:
struct first
{
char a[5];
int b;
}first= {0};
And I'm trying to create a array of this struct in another struct with each element in the array has the value of {0}:
struct second
{
first f_array[20];
};
However, when I compile, it says error: expected specifier-qualifier-list before 'first'.
Anyone knows how to create a struct array in other struct like what I'm trying to do?
This happens because the compiler can't resolve the type of first unless you include the keyword struct in the declaration of the array like this :
struct first f_array[20];
Or use typedef :
typedef struct {
char a[5];
int b;
} first;
struct second {
first f_array[20];
};
struct second obj = {{0}};
Using typedef means you no longer have to write struct all over the place and makes your code more cleaner.

"warning: useless storage class specifier in empty declaration" in struct

typedef struct item {
char *text;
int count;
struct item *next;
};
So I have this struct with nodes defined as above, but Im getting the error below and Im not able to figure out whats wrong.
warning: useless storage class specifier in empty declaration
};
I'm not sure, but try like that :
typedef struct item {
char *text;
int count;
struct item *next;
}item;
typedef is used to create a shorthand notation for an existing type in C. It is similar to #define but unlike it, typedef is interpreted by the compiler and offers more advanced capabilities than the preprocessor.
With its simplest form, typedef is given as
typedef existing_type new_type;
for instance,
typedef unsigned long UnsignedLong;
For example, if you trace the definition of size_t back to its root, you will see that
/* sys/x86/include/_types.h in FreeBSD */
/* this is machine dependent */
#ifdef __LP64__
typedef unsigned long __uint64_t;
#else
__extension__
typedef unsigned long long __uint64_t;
#endif
...
...
typedef __uint64_t __size_t;
and then
/* stddef.h */
typedef __size_t size_t;
which actually means, size_t is an alias for unsigned long long,depending on the 64-bit modal (LP64, ILP64, LLP64) your machines has.
For your question, you attempt to define a new type but do not name it. Don't let the struct item {..} definition confuse you, it is just a type you are declaring. If you replace the whole struct item {...} with a basic type, say with an int, and rewrite your typedef, you would end up something like this
typedef int; /* new type name is missing */
the correct form should be
typedef struct item {...} Item;
See the examples below for different structure definitions
#include <stdio.h>
/* a new type, namely Item, is defined here */
typedef struct item_t {
char *text;
int count;
struct item_t *next; /* you canot use Item here! */
} Item;
/* a structure definition below */
struct item {
char *text;
int count;
struct item *next;
};
/* an anonymous struct
* However, you cannot self-refence here
*/
struct {
int i;
char c;
} anon;
int main(void) {
/* a pointer to an instance of struct item */
struct item *pi;
/* Shorthand for struct item_t *iI */
Item *iI;
/* anonymoous structure */
anon.i = 9;
anon.c = 'x';
return 0;
}
The typedef is useless because you didn't give it a name. You cannot use the typedef in any way. That's why you get a warning, because the typedef is useless.
The struct is actually still usable without the warning if you remove the typedef keyword like this:
struct item {
char *text;
int count;
struct item *next;
};
You just need to include the 'struct' keyword in the variable declaration. i.e.
struct item head;
as other have pointed out if you include the name at the end of the struct definition then you can use it as the typedef and you get rid of the warning even without the struct keyword but this makes the first instance of 'item' superfluous i.e.
typedef struct {
char *text;
int count;
struct item *next;
} item;
item head;
will also get rid of the warning.
This is an oldie, but stumbled here because I had the same linker error.
I made the foolish mistake of naming a file with an extension .c, and should have been .cpp - created the SAME error - so watch out!
Renamed the file to have a .cpp extension, and the problem magically went away.
Took a while to figure that out, so happy to share my experience.

Using a struct in a header file "unknown type" error

I am using Kdevelop in Kubuntu.
I have declared a structure in my datasetup.h file:
#ifndef A_H
#define A_H
struct georeg_val {
int p;
double h;
double hfov;
double vfov;
};
#endif
Now when I use it in my main.c file
int main()
{
georeg_val gval;
read_data(gval); //this is in a .cpp file
}
I get the following error:
georeg_chain.c:7:3: error: unknown type name 'georeg_val'
(This is in the georeg_val gval; line)
I would appreciate if anyone could help me resolve this error.
In C one has two possibilities to declare structure:
struct STRUCT_NAME {} ;
or
typedef struct {} STRUCT_ALIAS;
If you use first method (give struct a name) - you must define variable by marking it explicitly being a struct:
struct STRUCT_NAME myStruct;
However if you use second method (give struct an alias) then you can omit struct identifier - compiler can deduce type of variable given only it's alias :
STRUCT_ALIAS myStruct;
Bonus points:
You can declare struct with both it's name and alias:
typedef struct STRUCT_TAG {} STRUCT_TAG;
// here STRUCT_NAME == STRUCT_ALIAS
Then in variable definition you can use either first or second method. Why both of two worlds is good ? Struct alias lets you to make struct variable definitions shorter - which is a good thing sometimes. But struct name let's you to make forward declarations. Which is indispensable tool in some cases - consider you have circular references between structs:
struct A {
struct B * b;
}
struct B {
struct A * a;
}
Besides that this architecture may be flawed - this circular definition will compile when structs are declared in the first way (with names) AND struct pointers are referenced explicitly by marking them as struct.
If you have to define a new type, you have to write:
typedef struct {
int p;
double h;
double hfov;
double vfov;
} georeg_val ;
Then you can use georeg_val as a new type.
Defining a struct type (on this example, a binary search tree struct):
struct tree {
int info;
struct tree *left;
struct tree *right;
}
typedef struct tree treeNode;
Declaring a function eg.:
treeNode *insertElement(treeNode *treeA, int number);

Attempting to define two dependable structures in C

I am trying to define two structures in C when the second struct uses the first as an array member and has two pointer members of itself.
Visual Studio does not like my code:
syntax error : '}'
syntax error : identifier 'tokenListNode'
syntax error : missing '{' before '*'
any idea how to solve this?
--> Please note that the errors appear with or without the declarations I added at the beginning of the code.
--> In addition, if someone can explain to me what is the difference between the identifier
before and after the struct's curly brackets, I'll be grateful.
Below is the code:
#define ARRAY_SIZE 100
struct tokenListNode;
struct TOKEN_LIST_NODE;
enum TOKEN_TYPE
{
id = 0,
INT_NUM,
INT_REAL,
ASSIGNMENT_OP,
RELATION_OP,
ARITHMETIC_OP
} tokenType;
typedef struct TOKEN
{
char* lexema;
enum TOKEN_TYPE type;
int lineNumber;
} token;
typedef struct TOKEN_LIST_NODE
{
token tokenArray[ARRAY_SIZE];
tokenListNode* prevNode;
tokenListNode* nextNode;
int tokenCounter;
}tokenListNode;
You do not define tokenListNode until after you use it. Change to the following:
typedef struct TOKEN_LIST_NODE tokenListNode;
struct TOKEN_LIST_NODE
{
token tokenArray[ARRAY_SIZE];
tokenListNode* prevNode;
tokenListNode* nextNode;
int tokenCounter;
};
The definition of a structure is composed of the keyword struct; the "struct tag"; and the struct members
struct tag { int member1; /* &c */ };
You can leave the tag out and create an unnamed structure ... why you would do so is another matter: you can't refer to the structure without a struct tag!
struct { int member1; /* &c */ };
Also, you can take any type and give it another name using typedef
typedef old_type new_name;
as in
typedef struct tag { int member1; /* &c */ } tag;
/* <------------ old type ------------> <new name> */
The above line defines a struct (named struct tag) and, at the same time, gives that type a new name: tag
what is the difference between the identifier before and after the struct's curly brackets
That's the result of mixing definition of struct and typedef. The name before the {} is the "tag" of the structure, the name after the {} is the new name for the type being typedef'd.
You need to use the struct tag instead of it's typedef'ed version when referring to itself.
struct TOKEN_LIST_NODE
{
token tokenArray[ARRAY_SIZE];
struct TOKEN_LIST_NODE* prevNode;
struct TOKEN_LIST_NODE* nextNode;
int tokenCounter;
};

Resources