typedef - does not name a type - c

i have two headers
in header “BinTree.h":
typedef struct node {
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode;
in header "Queue.h"(which includes BinTree.h):
typedef BTNode* Dataype;
at compiling the compilor said:
error: ‘BTNode’ does not name a type
What's wrong?

Did you include BinTree.h in Queue.h before the declaration?
Or have you .cpp (or moral equivalent) include it beforehand
EDIT FOR CDT
Forward declarations are the answer.
As you did not post the code it is difficult to tell.
But I would hazzard a guess here
typedef struct node BTNode;
whould hit the ticket in Queue.h

If you have mutual inclusion you need a forward declaration of your node type. Add this before the typedef: typedef struct node BTnode;

Related

Typedef of a type defined in a separate header file

I have two header files:
src/util/buffer.h:
//Namespace Src Util Buffer sub
struct sub_buffer{
size_t size;
void *buf;
};
//tons of static inline functions
src/lib_context.h:
//Namespace Src Lib Context slc
typedef struct sub_buffer slc_buffer; // Is this typedef ok?
struct slc_context{
//definition
};
void slc_set_buffer(slc_buffer *buf_ptr);
//tons of other structs and functions
The thing that I was not sure about was the typedef struct sub_buffer slc_buffer;. There was a choice to include the src/util/buffer.h, but that would intoroduce tightly coupling to the header and it would be more difficult to replace it with e.g. another buffer definition containing flexible array member.
Is it common to introduce such a typedef to the structure that is defined in another header file so its implementation will be provided in the c file when including the header (but not to include one header to another header file)?
No, that would be an error.
You probably meant
typedef struct sub_buffer slc_buffer;
in which case it's fine, you can always introduce typedef aliases to types, even without having those types defined in the scope you're in.
This is the reason the classical self-referencing "node" works:
typedef struct node node;
struct node {
node *next;
void *data;
};
Notice how on the first line a typedef for an unknown type is used.

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.

link list node in C, struct prototype

I am working on an embedded system and I need to implement a linked list.
So I used a struct to construct a node
typedef struct A
{
... //some data
struct A *next;
struct A *prev;
} A;
I think on PC (gcc) this works fine. However, the embedded system compiler complains that "identifier A is not declared"...
What is the best solution for this?
You should add a separate forward declaration of the struct:
struct A;
typedef struct A
{
... //some data
struct A *next;
struct A *prev;
} A;
Some compilers do take your definition the way you posted it, but I've seen older compilers that require a separate forward declaration. This may be related to an older standard, or an incomplete standard implementation. In fact, on a project where we needed to write code that runs on five platforms with different compilers, we made it a companywide coding standard requirement to have the forward declaration separate from the struct's typedef.
You can split it out:
typedef struct A A;
struct A {
... //some data
struct A *next;
struct A *prev;
};
The original code should compile just fine. You are probably using a non-standard compiler. As an alternative to the other suggestions, you can try this code:
typedef struct a
{
... //some data
struct a *next;
struct a *prev;
} A;
This way, you shouldn't need a forward declaration.

typedef struct clarity

I am getting confused with typedef can anyone transcribe this to a normal composition? of structures? I really don't want to handle typedef since it's gets me confuse
struct stackNode
{
int data;
struct stackNode *nxtptr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
is
typedef struct stackNode StackNode; is the same as struct stackNode StackNode
and typedef StackNode *StackNodePtr; is the same as struck stackNode *StackNodePtr??
If you don't want to use typedef you can always use full type name:
Instead of:
StackNode sn;
You would use:
struct stackNode sn;
Instead of:
StackNodePtr snp;
You would use:
struct stackNode *snp;
The declarations are exactly the same.
A more common way to write the very same would be:
typedef struct stackNode
{
int data;
struct stackNode *nxtptr;
} StackNode_t;
where stackNode is the so called "struct tag" and StackNode_t is the actual name of the type. If you declare structs like this, the rest of the program won't need to concern itself with the struct tag, and you can use nxtptr as if it was of StackNode_t.
typedef struct stackNode
{
int data;
struct stackNode *nxtptr;
} StackNode_t;
If you do not want to have "struct stackNode" inside the struct, I found that I am able to do this:
typedef struct STACKNODE STACKNODE;
typedef struct STACKNODE
{
int data;
STACKNODE *nxtptr;
};
That you could code such a thing, AND it compiles just fine, AND it runs just fine may seem counter intuitive. It can be counter intuitive because I'm using the same name for struct STACKNODE and the typedef STACKNODE. And further, I am typedef-ing the struct before it exists as a definition. Nevertheless, I have found that I can do this with Microsoft's C through many versions to today and Borland C (way back then).
I like it because if I am already going to typedef the struct, I don't like resorting to "struct STACKNODE *nxtptr" (i.e. using the word "struct") inside the struct definition.
With typedef, you define StackNode to be struct stackNode, and the Pointer StackNodePtr to be StackNode.
So what is not clear?
Well, to explain it easily: Typedef does basically nothing else then to tell the compiler that you create a new type of variables (such as int, char etc..)
The main reasons why to use typedef are
Mnemonic names. This is often used in the library functions as well. Instead of using a standard type like int or long you can just typedef it into a size_t. So it's clearer what this variable is used for.
Shortening names. Commonly used for things like structs (this would be your case). To avoid always having to type struct myStruct varname you can easily use a typedef to get rid of the struct in front.
see if typedef helps. More like a 101. The section labelled "Usage concerns" may give some insights into your, er, concerns.

error: expected declaration specifiers or ‘...’ before ‘list_node’

I have a catalog.h file with this
typedef struct node* list_node;
struct node
{
operationdesc op_ptr;
list_node next;
};
and a parser.h with this
#include "catalog.h"
int parse_query(char *input, list_node operation_list);
Both headers have #ifndef, #define, #endif.
The compiler gives me this error: expected declaration specifiers or ‘...’ before ‘list_node’ on the parse_query line.
What's the matter?
I tried to put the typedef in parser.h, and it's fine. Why do I get this error when the typedef is in catalog.h?
The error is this (from your comment):
I had an #include "parser.h" in the catalog.h. I removed it, and now it compiles normally...
Assuming that #include "parser.h" was before the typedef in catalog.h, and you have a source file that includes catalog.h before parser.h, then at the time the compiler includes parser.h, the typedef isn't available yet. It's probably best to rearrange the contents of the header files so that you don't have a circular dependency.
If this isn't an option, you can ensure that any source files that include these two files include parser.h first (or only).
Try this for catalog.h:
typedef struct node_struct {
operationdesc op_ptr;
struct node_struct* next;
} node;
typedef node* list_node;

Resources