I have the following struct in node.c:
typedef struct node {
char something[1024];
char onething[1024];
struct node *next;
} LList;
How would I define it in node.h?
Simply move that code to the header file, and include node.h in node.c.
Related
I'm a little confused about the structure of this struct. What is the point of typedef if you can name the struct to whatever you want without it? Is it also necessary to have " struct" in struct data_el *next when you are creating the next pointer; and wouldn't be confusing to also name that pointer the same name as the struct itself? Also, what is the point of having data_el after the bracket, when you can create a new struct in the program whenever you want, without naming an instance of it?
typedef struct data_el_{
int data;
struct data_el_ *next;
}data_el;
You can use struct like this:
struct data_el_ {
int data;
struct data_el_ *next;
};
struct data_el_ my_data_el;
But you can also typedef a struct:
typedef struct data_el_ {
int data;
struct data_el_ *next;
} data_el;
data_el my_data_el;
In the latter case, you can still use struct data_el_. However, inside the struct itself you must use struct data_el_.
For more information, check out the following related questions:
typedef struct vs struct definitions
Why should we typedef a struct so often 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.
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
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;
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.