"Expected '=', ',', ';', 'asm' or '__attribute__' before '->' token - c

So here's the code from the start to the lines my errors appear. I checked more than once every pointer relation and sintax rules, but for the last three lines the complier says:
"Expected '=', ',', ';', 'asm' or '__attribute__' before '->' token
What's wrong/missing?
Code:
typedef char labeltype;
typedef struct celltag{
labeltype label;
struct celltag* leftchild;
struct celltag* rightchild;
}celltype;
typedef celltype* BiTree;
typedef celltype* node;
node LAMBDA;
LAMBDA->label='A';
LAMBDA->leftchild=NULL;
LAMBDA->rightchild=NULL;

You don't have any functions in your code, and Instructions can only appear in functions.
The following lines are Instructions, which must be in a function:
LAMBDA->label='A';
LAMBDA->leftchild=NULL;
LAMBDA->rightchild=NULL;
I recommend putting these instructions into a main function:
typedef char labeltype;
typedef struct celltag{
labeltype label;
struct celltag* leftchild;
struct celltag* rightchild;
}celltype;
typedef celltype* BiTree;
typedef celltype* node;
int main(void) /* This is where a function starts */
{
node LAMBDA;
printf("Program Start\n");
LAMBDA->label='A';
LAMBDA->leftchild=NULL;
LAMBDA->rightchild=NULL;
printf("Program End\n");
return 0;
} /* This is the end of the function */

In C language all executable code is written inside functions. You can't just write statements in the middle of the file.
At file level in C you can only write declarations. Everything in your code are declarations, until you get to the last three lines. The last three lines are not declarations. You can't write them at file level.

Related

expected declaration specifiers or '...' before 'record_t' in header files c

I'm new to this header files in C.
What I'm trying to do is using makefile to compile my code files together. I have 2 header files and 2 c files for each header, also 1 main.c file.
I have main.c which is my main function and it has "#include "dict2.h"". dict1 and dict2 headers are somewhat the same. the difference is dict2 has additional linked list function.
-bash-4.1$ make dict1
gcc -Wall -c main.c -o main.o -g
In file included from main.c:6:
dict2.h:1: warning: useless storage class specifier in empty declaration
dict2.h:8: warning: useless storage class specifier in empty declaration
dict2.h:21: error: expected declaration specifiers or '...' before 'record_t'
dict2.h:24: error: expected declaration specifiers or '...' before 'record_t'
dict2.h:42: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
dict2.h:45: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
dict2.h:48: error: expected ')' before '*' token
make: *** [main.o] Error 1
my dict2.h funtion is looking like this:
typedef struct record_t;
typedef struct node_list node_list_t;
struct node_list;
typedef struct list_t;
typedef struct node node_t;
struct node;
node_t* transform_input(FILE *finput, node_t *root);
//line 21
node_t* bst_insert(node_t *root, record_t* data);
//line 24
node_t* bst_create_node(node_t* root, record_t* data);
node_t* bst_search(node_t* root, char* name_keyword, int* numcomparison);
void search_then_print(char* keyword, node_t* root, FILE* foutput, \
int* numcomparison);
void freeTree(node_t *root);
void print_record(FILE* foutput, node_t* targetnode, char* keyword,\
int* numcomparison);
//line 42
list_t *insert_at_foot(list_t *list, record_t *datarecord);
//line 45
list_t *create_empty_list(void);
//line 48
void free_list(list_t* list);
I've looked at the discussion online but and trying to fix it but I couldn't find the errors in the header file.
Thank you for your help.
Statements:
typedef struct record_t;
typedef struct list_t;
miss the typenames.
Should probably be:
typedef struct record record_t;
typedef struct list list_t;
In the following, I use struct tag vs. type identifier. I googled a bit and found a (IMHO) quite easy to understand explanation on Wikipedia.
Some programmer dude mentioned this but it seems you didn't understand his point. So, I elaborate this a little bit:
This is valid:
struct Node {
struct Node *pNext;
};
and can be used with struct:
void insert_after(struct Node *pNode);
For whom which are to lazy to type the struct always a typedef can help:
struct Node {
struct Node *pNext;
};
typedef struct Node Node;
It might look confusing but the compiler separates struct tags and types in separate lists. Thus, the first and second Node is no identifier "collision".
Both can be done in at once:
typedef struct Node {
struct Node *pNext;
} Node;
Assuming another case without "recursive" usage of type, the struct tag can even be left out:
typedef struct {
int year, month, day;
} Date;
This is a struct type which can be used exclusively without struct keyword.
I assume this was intended when writing
typedef struct record_t;
but the compiler interpretes it not as the writer might intend it. The compiler reads this as struct with tag record_t and a missing type identifier.
This is how I read the
warning: useless storage class specifier in empty declaration
(It helps to know that typedef is syntactically handled in the compiler like static and extern and, thus, counted as storage class though this seems not quite obviously to everybody.)
I must admit that I don't know how to interprete the
error: expected declaration specifiers or '...' before 'record_t'
but I would ignore this and just fix the weakness in the typedef (and count the error as follow-up error.)
I also must admit that I have no idea how to solve this with an anonymous struct and go with the idea of a struct with a tag which is "re-used" as type identifier:
#include <stdio.h>
/* typedef for an incomplete struct */
typedef struct Date Date;
/* use incomplete struct type for prototype of function */
void printDate(Date *pDate);
/* define the complete struct */
typedef struct Date {
int year, month, day;
} Date;
/* implementation of function */
void printDate(Date *pDate)
{
printf("%04d/%02d/%02d", pDate->year, pDate->month, pDate->day);
}
/* check this out */
int main(void)
{
Date date = { 2018, 9, 3 };
printDate(&date);
return 0;
}
Output:
2018/09/03
Live Demo on ideone

Error when I try to declare bag struct?

bag-implementation.h:
typedef struct node {
struct node *next;
char *element;
int repeats;
} Node;
typedef struct{
size_t size;
Node *head;
}Bag;
Line that errors in bag.c (which includes bag.h which includes bag-implementation.h):
Bag bag_union(Bag bag1, Bag bag2){
Bag union;
return bag1;
}
Error:
bag.c: In function 'bag_union':
bag.c:188:12: error: expected '{' before ';' token
bag.c:188:7: error: two or more data types in declaration specifiers
make: *** [bag.o] Error 1
If I try to compile without creating that bag, then it works fine. What is the issue?
union is a reserved word in C, so you can't have a variable called like this. Simply rename it.
union is a keyword it can't be used for variable.
This is the rule to define a variable.

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

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

Error: expected specifier-qualifier-list before ‘TR’

I have a problem defining my structure inside the union on Bison
I made a structure
typedef enum {Binary_Op,Uni_Op,Variable, Const} Tag_Type;
typedef struct tree
{
Tag_Type Tag;
union
{
struct
{
char op;
struct tree *left, *right;
}bin_op;
struct
{
char op;
struct tree *arg; /* negative or positive */
}uni_op;
char var;
int const_val;
}u;
}TREE_REC, *TR;
%}
%union
{
int y_int;
TR y_tree;
}
%type <y_tree> expr term factor assign
%token <y_int> CONST
%token <y_int> VAR
%%
but inside the union TR has an error. I don't understand why!! any help?
You need to define struct tree and TR in a header file that you #include before you #include "y.tab.h". The error message is telling you that you're trying to use TR before the compiler has seen a definition for it.
I'm a bit confused with your typedef struct tree {...} TREE_REC, *TR. I would have rather written :
typedef struct tree {...} TREE_REC; //Alias on struct tree
typedef TREE_REC * TR; //Definition of the pointer to a struct tree
The , in your typedef is disturbing me.
Can you test my solution, or just clarify the syntax of your typedef?

Categories

Resources