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?
Related
I came across this solution for the problem and it solved by forward referencing the struct.
But I've a different scenario, in my case the struct is embedded. My actual problem has a long dependency chain, a -> b -> c -> d -> a (-> means includes or depends upon) so I'm going to use the given solution from the post.
Q. Is it possible to have embedded type struct edgelist edges?
Error: 1field has incomplete type 'struct edgeList'
struct edgelist;
typedef struct
{
char* name;
float x, y;
struct edgelist* edges;
} vertex;
This is an incomplete type:
struct edgelist;
This code is correct because it's allowed to have a pointer to an incomplete type:
typedef struct
{
char* name;
float x, y;
struct edgelist* edges; // you can have pointers to incomplete types
} vertex;
But as soon as you use this, your code becomes incorrect because edges is not a pointer:
struct edgelist edges;
In order to be able to use a variable of type struct edgelist, the struct needs to be defined somewhere.
So this would be correct:
struct edgelist // declares an actual (complete) type struct edgelist
{
// whatever declaration you want
};
struct edgelist edges;
I was looking at this example and I found out that there is the declaration
struct edge
{
int x;
int y;
int weight;
struct edge *link;
}*front = NULL;
What does this actually mean? Is it possible to create a structure which is also a pointer with the name front and it NULL...?
A struct is just another C type, as such, the variables it is used to define, may be created as normal instances, or pointers:
int a, *pA=NULL; //normal instance, pointer instance
struct edge
{
int x;
int y;
int weight;
struct edge *link;
}sEdge, *front = NULL; //normal instance, pointer instance
And, as with any pointer variable, needs to be pointed to owned memory before it can be safely used: (examples)
int main(void)
{
// both variable types are handled the same way...
pA = &a; //point pointer variable to normal instance of `int a`
front = &sEdge;//point pointer `front` to instance of 'struct edge'
//allocate memory, resulting in assigned address with associated memory.
pA = malloc(sizeof(*pA));
front = malloc(sizeof(*front));
...
EDIT to answer question in comments:
This small example throws no error or warning. (Edit your question above, or better yet, post another question showing details of what you are seeing.)
struct edge
{
int x;
int y;
int weight;
struct edge *link;
}*front = '\0';
int main(void)
{
struct edge tree[10];
return 0;
}
It is a pointer to a struct and a declaration of a new type called struct edge.
Maybe that would put some more light, when you write:
struct edge
{
int x;
int y;
int weight;
struct edge *link;
};
You are saying: I'm creatig struct edge, which I will use to define objects of this struct by typing:
struct edge edgeObject;
But when you write:
struct edge
{
int x;
int y;
int weight;
struct edge *link;
} edgeObject;
You are saying: I'm creating struct edge and at the same time I'm defining edgeObject which is of type struct edge.
And this allows you to use that object directly as it is already defined:
edgeObject.x = 0;
So going back to your example you are saying: I'm creating structure edge and at the same I'm defining pointer to that struct front which is set to NULL.
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 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.
How can I have a pointer to the next struct in the definition of this struct:
typedef struct A {
int a;
int b;
A* next;
} A;
this is how I first wrote it but it does not work.
You can define the typedef and forward declare the struct first in one statement, and then define the struct in a subsequent definition.
typedef struct A A;
struct A
{
int a;
int b;
A* next;
};
Edit: As others have mentioned, without the forward declaration the struct name is still valid inside the struct definition (i.e. you can used struct A), but the typedef is not available until after the typedef definition is complete (so using just A wouldn't be valid). This may not matter too much with just one pointer member, but if you have a complex data structure with lots of self-type pointers, may be less wieldy.
In addition to the first answer, without a typedef and forward declaration, this should be fine too.
struct A
{
int a;
int b;
struct A *next;
};
You are missing the struct before the A*
typedef struct A {
int a;
int b;
struct A* next;
} A;
You can go without forward declaration:
struct A {
int a;
int b;
struct A *next;
};
Please, you're in C, not C++.
If you really must typedef a struct (and most programmers that I work with would not¹), do this:
typedef struct _A {
int a;
int b;
struct _A *next;
} A;
to clearly differentiate between _A (in the struct namespace) and A (in the type namespace).
¹typedef hides the size and storage of the type it points to ― the argument (and I agree) is that in a low-level language like C, trying to hide anything is harmful and counterproductive. Get used to typing struct A whenever you mean struct A.
typedef struct {
values
} NAME;
This is shorter way to typedef a struct i think its the easiest notation, just don't put the name infront but behind.
you can then call it like
NAME n;
NAME *n; // if you'd like a ptr to it.
Anything wrong with this approach?