Edit 2
Thanks for all the suggestions, I edited the code below from the suggestions given. However, it still doesnt seem to compile. But nevertheless, thanks a lot for the help hands.
Edit
I apologize for not putting the pcb struct into the code snippet. There is a struct called pcb defined in above the two structs I originally posted. Namely,
typedef struct pcb{
UINT32 proc;
struct pcb *link;
}pcb;
Hi,
I asked a question regarding structs in C a few minutes ago and got an answer blazing fast. But now I'm facing another problem, namely the error in the title of this question. I'm trying to implement a simple priority queue in C using arrays of queues. However, when I try to declare a function on pcb_pQ structure, I get the above error. I have the structs clearly defined in the heard file.
In the header file:
typedef struct pcb_Q{
pcb *head;
pcb *tail;
SINT32 size;
} pcb_Q;
typedef struct pcb_pQ {
pcb_Q queues[5];
SINT32 size;
} pcb_pQ;
Function prototype in header file:
/*priority queue operations*/
VOID pcb_pq_enqueue(pcb_pQ*, pcb*);
Function impelmentation in .c file:
VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ_p, pcb* pcb_p) {
pcb_Q* pcb_Q_p;
int priority;
priority = pcb->proc_priority;
pcb_Q_p = &pcb_pQ->queues[priority];
pcb_enqueue(pcb_Q_p, pcb);
}
When I try to compile the above code, I get an "error: expected ')' before '*' token". This error is pointing to the function signature in the .c file, namely
VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ_p, pcb* pcb_p) {
But I am not sure why I am getting this error, could someone give me a hand? Thanks a lot.
Are you including the header file? Barring misspellings, that error is almost invariably caused by a missing typedef.
In other words, the compiler doesn't know about either the pcb_pQ or pcb type (or both).
Edit: There's something else wrong because this compiles fine:
qq.h
typedef struct pcb {
unsigned int proc;
struct pcb *link;
} pcb;
typedef struct{
pcb *head;
pcb *tail;
int size;
} pcb_Q;
typedef struct pcb_pQ {
pcb_Q queues[5];
int size;
} pcb_pQ;
void pcb_pq_enqueue(pcb_pQ*, pcb*);
qq.c:
#include <stdio.h>
#include "qq.h"
void pcb_pq_enqueue(pcb_pQ *pcb_pQ, pcb *pcb) {}
int main (void) { return 0; }
I had to use other types (and I modified the pcb structure to be a named one - I'm not sure your given one should have compiled since as-is since there is no struct pcb type in existence).
Based on all the comments and answers to date, I'm pretty certain there's something wrong with your compiler. GCC compiles that snippet above just fine.
Try putting my two files above onto your system and seeing if they compile okay.
And be aware that you do need to name your pcb structure. See here for the gory details but the consensus seems to be that
typedef struct {
unsigned int proc;
struct pcb *link;
} pcb;
will define a struct pcb incomplete type which is a distinct type from pcb (and the structure you're currently defining).
It looks like the compiler doesn't find the defintion of pcb.
Change:
typedef struct{
UINT32 proc;
struct pcb *link;
}pcb;
to:
typedef struct pcb {
UINT32 proc;
struct pcb *link;
} pcb;
First, it is 'void' not 'VOID' until or unless you have definition like
#define VOID void
This can be better if
typedef struct tag_struct_pcb_Q {
pcb *head;
pcb *tail;
SINT32 size;
} pcb_Q;
typedef struct tag_structpcb_pQ {
pcb_Q queues[5];
SINT32 size;
} pcb_pQ;
modern 'C' compiler expect the return type must be mentioned.
If you have definition like this also cause error.
#define VOID
Another possibility
VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ, pcb* pcb) {
where does pcb is from? it is not good to have pcb* pcb. ALWAYS give different name for variable type and variable name
Related
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.
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 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.
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?
I'm trying to implement a simple priority queue from array of queues. I'm trying to define a struct queue, and than a struct priority queue that has an array of queues as its member variable. However, when I try to compile the code, I get the following error:
pcb.h:30: error: array type has incomplete element type
The code is below:
typedef struct{
pcb *head;
pcb *tail;
SINT32 size;
} pcb_Q;
typedef struct {
struct pcb_Q queues[5];
SINT32 size;
} pcb_pQ;
Could someone give me a hand? Thanks a lot.
You already typedef the pcb_Q, no need to use struct keyword any more.
Just use this:
typedef struct {
pcb_Q queues[5];
SINT32 size;
} pcb_pQ;
typedef struct {
pcb_Q queues[5];
SINT32 size;
} pcb_pQ;
Your struct type has no name. Only the typedef is called pcb_Q.
I don't like this line:
struct pcb_Q queues[5];
Which references structure pcb_Q.
You have not defined pcb_Q as a structure.
Instead, you typedef'd pcb_Q as a new type (which happens to be an un-named struct).
Try this instead:
pcb_Q queues[5];