typedef error under gcc - c

in my code, I put following codes
typedef Status int;
I got following errors, it is expected '=', ',', ';', 'asm' or 'attribute' before 'int' under linux.
I can't find what's probelm. Thanks for your help.
a

Use:
typedef int Status;
instead of
typedef Status int;
The syntax of a typedef is the same as the syntax as any ordinary declaration:
int a, b; // declare int objects a and b
typedef int c, d; // declare int type-aliases c and d

The typedef should be followed by the type and then the name. Therefore, the typedef should look like this:
typedef int Status;

The syntax for typedef is
typedef <SOME_TYPE> new_name_for_some_type;
You are swapping the <SOME_TYPE> and new_name_for_some_type elements of the typedef syntax.

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

expected specifier-qualifier-list before 'typedef' - one H file with a typedef of a struct while its fields are written in another H

i'm receiving the next error:
expected specifier-qualifier-list before 'typedef' (in challenge_room_system_fields.h):
This is challenge_system.h:
typedef struct SChallengeRoomSystem
{
#include "challenge_room_system_fields.h"
} ChallengeRoomSystem;
This is challenge_room_system_fields.h:
#include "challenge_system.h"
typedef struct SChallengeRoomSystem //this is where i get the error
{
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
} ChallengeRoomSystem;
Can someone help me figure out what's wrong?
I know its not the best way to deal with structs but as a school assignment i'm required not to change the challenge_system.h. I am allowed to change only challenge_room_system_fields.h
Thanks in advance....
i should've just write the following inside challenge_room_system_fields.h :
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
The reason is that the #include by definition takes the included file and Copy-Pastes it instead of the line I wrote #include...
so eventually challenge_system.h will look as follows:
typedef struct SChallengeRoomSystem
{
// this is where previously i had: #include "challenge_room_system_fields.h"
//now i will have the following:
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
} ChallengeRoomSystem;
And now it works:)

c structures that contain function pointers

I declare all my structures using the following macro..
#define structure typedef struct
with this I declare a structure , for example
structure
{
int foo;
int(*proc)(void*);
}
_myobject;
the member proc , really received the argument which is a pointer
to the structure it belongs to ..
e.g.
myobj.proc(&myobj);
My question is , how can I declare the member proc with the type being
passed to it as a structure , and not a void * .. I know it doesn't make
a difference, this is only for aesthetics, as I have spent a lot of time
keeping my code clean..
structure
{
int foo
int(*foo)(_myobject*);
}
_myobject;
produces:
error: expected ':', ',', ';', '}' or '__attribute__' before 'int'|
You're attempting to refer to your struct recursively, which doesn't play nice with typedefs. One of the following should do:
typedef struct _myobject _myobject;
struct _myobject {
int foo;
int (*proc)(_myobject*);
};
...
typedef struct _myobject {
int foo;
int (*proc)(struct _myobject*);
} _myobject;
edit: If you want to continue using structure as much as possible (though really, other than for consistency with the existing code, I'm not sure why you would want to), you could do:
structure _myobject _myobject;
struct _myobject {
int foo;
int (*proc)(_myobject*);
};
...
structure _myobject {
int foo;
int (*proc)(struct _myobject*);
} _myobject;
Though both of these will require use of the struct identifier, if you wanted to avoid that.
You can change the macro a little:
#define structure(x,y) typedef struct x y x
structure (_myobject,
{
int foo;
int(*proc)(struct _myobject*);
}
);
which looks bad to me. Another way to do this is:
#define structure typedef struct
structure _myobject
{
int foo;
int(*proc)(struct _myobject*);
}
_myobject;

please explain the behaviour of typedef here

int main()
{
int a;
typedef struct
{
int i;
int j;
}type1;
typedef type1 type[10]; //Please explain this line ?
typedef struct
{
int l;
type c;
}type2;
type2 x;
x.c[0].i=1; //How can we write this??
x.c[0].j=2;
x.c[2].j=3;
printf("%d",x.c[2].j);
return 0;
}
Program is compiling successfully which I'm expectecting not to because of
typedef type1 type[10];
Please explain the behavior of typeded here.All I know is that we can define an alias with the help of typedef.
output:3
The way to read typedef is as a regular variable declaration, with the variable's type being the type that is being given an alias, and the variable name being the name of the new alias.
So, in
typedef type1 type[10];
if we drop the typedef we get:
type1 type[10];
This clearly defines type to be an array of 10 type1. So, the typedef is then introducing the name type for the type "array of 10 type1".

C struct/function cross reference

So I'm morelikely running in a cross reference issue in C
Hello, (I couldnt write it in first for some reason)
Basicly this code:
structA.h:
#pragma once
#include "structB.h"
typedef struct
{
B b;
}A;
structB.h:
#pragma once
#include "structA.h"
typedef struct
{
int field;
}B;
void func(A* a);
structB.c:
#include "structB.h"
void func(A* a)
{
}
Produce the follwing errors on VC2010:
structa.h(7): error C2016: C requires that a struct or union has at
least one member structa.h(7): error C2061: syntax error : identifier
'B' etc
so since I only have a pointer to A in func(A* a) I try doing a forward declaration like this:
#pragma once
typedef struct A;
typedef struct
{
int field;
}B;
void func(A* a);
and I add #include "structA.h" in structB.c
However this doesnt work, to fix it I have to change the param of func(A* a) to func(struct A* a) in prototype and implementation...
But in this case i lose the purpose of typedef-ing my structs...
I know i could simply move the function to another file, but the function is related to my structure so I'd like to keep the prototype in the same file than my struct.
Now maybe thats not a good way to do thing in C, i'm mostly used to C++ so i tend to think in C++ when doing C wich is often problematic...
Does someone know a workaround? Thank you very much.
typedef struct structA;
How did this even compile? -- Correctly:
typedef struct A A;

Resources