Syntax error identifier when defining a struct in C - 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.

Related

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.

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

Pointer typecasting in c

I am writing an implementation of graphs in C language. I came across a situation where I am not able to figure out the reason for the way the compiler is behaving with a pointer typecast warning.
Here are the structures;
#define MAXV 10
typedef struct {
int y;
int weight;
struct edgenode *next;
} edgenode;
typedef struct {
edgenode *edge[MAXV+1];
int degree[MAXV+1];
// other info of graph
} graph;
// operation in some other function
p->next = g->edge[x];
I got a pointer typecast warning[enabled by default] when I do this kind of operation.
I was not able to remove this warning even after trying to typecast with every possible cast.
Finally I made a code change in the structure and suddenly the warning was gone.
The structure code change was this:-
typedef struct edgenode { // note that I have added structure name here
// same as above definition
} edgenode;
// operation in some other function
p->next = g->edge[x];
Now the warning is gone and code runs without any warnings.
I do not understand why is this happening; can anybody help me with this problem?
The problem is here:
typedef struct {
int y;
int weight;
struct edgenode *next;
} edgenode;
It is not clear what type struct edgenode *next; is referring to (it doesn't matter; somewhere, presumably, there's a struct edgenode defined), but it is not this structure because it has no tag. You need:
typedef struct edgenode
{
int y;
int weight;
struct edgenode *next;
} edgenode;
Now the pointer refers to another structure of this same type. So, the fix you found was the correct fix for your problem.
Remember: a typedef is an alias (alternative name) for an existing type. You created a type name edgenode, but you had not defined the type struct edgenode. You don't have to fully define a structure type before you create pointers to it; this can be a good way of creating 'opaque types'.
The other way to define things is:
typedef struct edgenode edgenode;
struct edgenode
{
int y;
int weight;
edgenode *next;
};
This says that the type name edgenode is an alias for a struct edgenode; the structure definition then tells the compiler what a struct edgenode looks like.

missing '(' before '*'

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;
};

C: structs in structs

My assignment is to create a simple graph that has both Nodes and Edges.
In my header file which was given and cant't be modified I have
typedef struct Edge_s* Edge;
typedef struct Node_s* Node;
typedef struct Graph_s* Graph;
and in my graph.c
typedef struct{
size_t w;
struct Node_s* target;
}*Edge;
typedef struct{
size_t value;
Edge* edges;
size_t s;
}*Node;
typedef struct{
Node* nodes;
size_t n;
Edge* edges;
size_t e;
}*Graph;
Edge create_edge(Node t, size_t w){
Edge ret = malloc(sizeof(*ret));
ret->target = t;
ret->w = w;
return ret;
}
This gives a warning on compile
warning: assignment from incompatible pointer type
I'm kind of confused here, what am I getting wrong and how should I fix it? The program is almost working and I'm getting one strange bug that I believe might be because of this.
Your typedef-definitions are mixed up badly. I'm surprised it even compiles.
You first defined typedef-name Edge as
typedef struct Edge_s* Edge;
and then later re-defined it as
typedef struct{
size_t w;
struct Node_s* target;
}*Edge;
These two definitions define Edge in two completely unrelated ways. (All C compilers I know would immediately report an error if the first group of declarations would meet the the second group in the same translation unit.)
I'd say that your second struct definition should be simply
struct Edge_s {
size_t w;
struct Node_s* target;
};
Don't attempt to redefine an existing typedef-name. It is simply illegal in C.
Ask yourself what type of object does ret->target point to and what type of object is it? Are they the same types of objects?

Resources