missing '(' before '*' - c

I'm working on a linked list for school and I am getting a ton of errors. I'm sure there's probably only one thing wrong with my code, but I can't seem to find it. I've commented out most of my code so I didn't have to paste like 200 lines in here and the main error is still showing up, although quite a few less times.
The error is:
error C2143: syntax error : missing '{' before '*'
I had probably 50-75 errors pop up along those guidelines before I commented out my code, but there are still a few with this code. Any help would be much appreciated.
//main.c
#define BUFFER_SIZE 1000
#include<stdio.h>
#include<stdlib.h>
#include"ListElmt.h"
#include"List.h"
#include"ListData.h"
int main(int argc, char *argv[]){
}
//List.c
#include<stdlib.h>
#include"List.h"
#include"ListElmt.h"
#include"ListData.h"
//List.h
struct List{
int size;
struct ListElmt *head;
struct ListElmt *tail;
};
//ListData.h
struct ListData {
int hour;
int min;
double temp;
int AC;
};
//ListElmt.h
struct ListElmt {
ListData *data;
ListElmt *next;
ListElmt *prev;
};

You need to forward declare structures if they aren't declared in the header file.
Therefore, List.h needs a forward declaration of struct ListElmt, and ListElmt.h needs a forward declaration of struct ListData.
Furthermore, in C you have to use struct before ListData and ListElmt in ListElmt.h since struct names aren't considered type names unless you use an explicit typedef.

you missed the struct keyword
struct ListElmt {
struct ListData *data;
struct ListElmt *next;
struct ListElmt *prev;
};

Related

Syntax error identifier when defining a struct in C

I have defined these structs in a C file and I don't know why I get this compile error.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<string.h>
//using namespace std;
typedef struct Gradina{
char* denumire;
int nrFlori;
float* preturi;
}Gradina;
typedef struct Nod {
Gradina* info;
Nod* next, * prev; //the first syntax error points here
}Nod; //the second error points here
I'm getting 37 syntax errors and I don't know why.
I'm using Visual Studio 2022.
typedef struct Nod {
Garden* info;
Nod* next, * prev;
}Nod;
The type Nod does not exist before the final ;, especially on the 3rd line above. You need
struct Nod* next, * prev;
where, even though struct Nod is not defined yet, you can use pointers to it.

Pointer in typedef struct confusion

I am trying to define a typedef struct as follows:
typedef struct node{
int keys[2*MIN_DEGREE-1];
struct node* child[2*MIN_DEGREE];
int numkeys;
int isleaf;
} BNODE,*BNODEPTR;
Instead of using struct node* child[2*MIN_DEGREE] why can't I declare the struct as follows:
typedef struct node{
int keys[2*MIN_DEGREE-1];
BNODEPTR child[2*MIN_DEGREE];
int numkeys;
int isleaf;
} BNODE,*BNODEPTR;
I am little confused as to how the compiler resolves structs that has nested pointers to the same type. It will be great somebody helps me clear this up.
Thanks
You can't use BNODEPTR in the body of the structure like that because it either doesn't exist as a type at all until after the definition after the close brace of the structure body, or (worse) it refers to a different type altogether*.
You could use:
typedef struct node BNODE, *BNODEPTR;
struct node
{
int keys[2*MIN_DEGREE-1];
BNODEPTR child[2*MIN_DEGREE];
int numkeys;
int isleaf;
};
And there's another whole argument that says BNODEPTR is evil and you should only use BNODE and BNODE *, but that's a style issue, not a technical one.
Were it my code, it would probably be more like:
typedef struct Node Node;
struct Node
{
int keys[2*MIN_DEGREE-1];
Node *child[2*MIN_DEGREE];
int numkeys;
int isleaf;
};
In C++, the rules are slightly different and you would not need the typedef line (so Node would be known as a type from the open brace).
* This can only happen if the original BNODEPTR is defined at an outer scope and this one appears inside a function, but when it happens, it is really confusing!
Instead of using struct node* child[2*MIN_DEGREE] why can't I declare
the struct as follows: BNODEPTR child[2*MIN_DEGREE];?
At that point, compiler (yet) does not know what the symbol BNODEPTR is.

Nesting Structs in C

I'm a noob at C and need some help getting my code to...welll...do anything. I have the following put into a .h file.
typedef struct
{
int active;
int dead;
ucontext_t t;
struct node *link;
}gtthread_t;
struct node{
struct gtthread_t thread;
};
typedef struct
{
int rubic;
}gtthread_mutex_t;
This is in a .h file... a .h file where I have had to #include ucontext.h ... which I know I am also not supposed to do... but it's the only way I can access ucontext_t, I find.
So, my error here is "field thread has incomplete type".
Why? What does that even mean? And how can I possible NOT import ucontext.h if I want to declare structs with that kind of data in the .h file?
Has nothing to do with your other include. This has to do with the fact that your first struct is anonymous and has a type name gtthread_t. C unlike C++ distinguishes between gtthread_t and struct gtthread_t.
Either name the struct:
struct gtthread_t
{
int active;
int dead;
ucontext_t t;
struct node *link;
};
Or change the type name to the typedef:
struct node{
gtthread_t thread;
};
The ucontext.h header only pre-declares the ucontext_t type. You're only supposed to use pointers to it, since its size is not known. Change your code to:
ucontext_t *t;
and you will be fine. Notice that all the functions in the ucontext.h header use such pointers to refer to contexts. This is common in C API design, since it allows the representation to be hidden inside the API's implmentation, which is nice.
One problem is that struct node has not been declared yet, so the gtthread_t type cannot be completed.
typedef struct
{
int active;
int dead;
ucontext_t t;
struct node *link;
}gtthread_t;
struct node{
struct gtthread_t thread;
};
You'll need a forward declaration of struct node:
struct node;
typedef struct
{
int active;
int dead;
ucontext_t t;
struct node *link;
}gtthread_t;
struct node{
struct gtthread_t thread;
};
Try that and see if it makes a difference.

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!
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

LinkedList, struct inclusion issue

liststructs.h:
struct _data_object {
int temp;
int interval_length;
};
typedef struct _data_object temp_data_object;
struct _list_node {
data_object *temp_data;
struct _list_node *prev;
struct _list_node *next;
};
typedef struct _list_node list_node;
struct _list {
int time;
list_node *head;
list_node *tail;
};
typedef struct _list list;
list.h:
list_node *alloc_node(int temp, int interval_length);
list_node *alloc_dummy_node(void);
list *alloc_temp_list(void);
void delete_first(list *list);
void insert_node(list *list, list_node *new_node);
void insert(list *list, int temperature, int interval);
I then use this in another file called calculations.c and in main.c, but then I declare extern list *xs; in calculations.h (it is defined in calculations.c) it complains:
Error[Pe020]: identifier "list" is undefined
I have included liststructs.h and list.h in that order in calculations.c and main.c and want to use xs in calculations and main.
Also:
What is better? To have structs and listoperations declared in the same header or separate them?
Protect your include files with #include safeguards, include liststructs.h in list.h, and both files in calculations.h. Safeguards in header files are typically written as:
#ifndef _XXXX_H_ // XXXX = LIST, LISTSTRUCT etc
#define _XXXX_H_
// definitions for file XXXX.h
#endif /* _XXXX_H_ */
From what you've told us, you have extern list *xs; declared in calculations.h, but didn't mention having included liststructs.h before that line which defines the identifier list.
liststructs.h needs to be included anywhere before you use the identifier list, just as list.h must be included before you attempt to call any of the functions it declares.
As long as you have include/header guards don't worry about including header files multiple times in a translation unit.

Resources