C structure that ends with an asterisk - c

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.

Related

Getting value from struct by pointer to pointer in C

I'm making a program in C. Basically I have a struct that contains some fields, and an array of pointers. Each struct has an pointer array that points to another struct forming a "connection" between them. I'm trying to get the value of a field that is stored at the memory address the pointer that is pointing at it.
Suppose this. I have two of these nodes in memory. A and B. A has a pointer inside A's array that is a reference to B. To get this pointer I'd have to do something like this:
*temp_ptr = (*ptr).pointer_array[0]
This would get the pointer address *ptr and give it to *temp_ptr.
Now what I am wondering is this. How can i do this? When I try this, I get "Expression must have struct or union type"
When I try this in lets say Java I could do this
int variable = field[0].fieldIWantToGet
I'd get the desired outcome.
Heres an image to clarify the intended behaviour that I'm trying to get. Link to behavior
Where Struct A is in a "global" collection of structs and has a array of pointers that lead to other Structs, such as B
Here is some code from my project.
#define GLOBAL_PTR_ARRAY_SIZE 10
Node* global_node_array[10];
typedef struct Node{
unsigned char node_id;
int *ptr_array[10];
int ptr_array_size;
}Node;
void append_connection(short position, short destination) {
Node* position_ptr = global_node_array[position];
Node* destination_ptr = global_node_array[destination];
if ((*position_ptr).ptr_array_size < GLOBAL_PTR_ARRAY_SIZE) {
int current_ptr_array_size = (*position_ptr).ptr_array_size;
(*position_ptr).ptr_array[current_ptr_array_size] = destination_ptr;
(*position_ptr).ptr_array_size++;
}
void print_id(Node* ptr) {
node* dptr = NULL;
dptr = ptr->ptr_array[0];
pptr = (int) (*ptr).ptr_array[0];
fprintf(stdout, "%d connection to %d exists", (*ptr).node_id, dptr-
>node_id);
}
int main(int argc, char const *argv[])
{
append_connection(0,1);
print_id(global_node_array[0]);
return 0;
}
Your picture shows array of the structs not the pointers. But the example below covers both.
struct a{
int field1,field2;
}
struct b{
struct a m[10];
}
struct c{
struct a *m[10]
}
/* and usage */
struct c *x;
struct b *y;
x -> m[5].field1;
y -> m[5] -> fileld1;

Pointer typecasting in c

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.

C: structs in structs

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?

C - Loading a Struct Containing a Pointer to a Pointer

This produces an incompatibility warning:
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
int key;
int data;
struct htData_* next;
struct htData_* prev;
}htData_;
typedef struct
{
int num_entries;
struct htData_** entries;
}ht_;
ht_* new_ht(int num_entries);
int ht_add(ht_* ht_p, int key, int data);
int main()
{
int num_entries = 20;
//crate a hash table and corresponding reference
ht_* ht_p = new_ht(num_entries);
//add data to the hash table
int key = 1305;
ht_add(ht_p,key%num_entries,20);
return 0;
}
ht_* new_ht(int num_entries)
{
ht_ *ht_p;
ht_ ht;
ht.num_entries = num_entries;
ht_p = &ht;
//create an array of htData
htData_ *htDataArray;
htDataArray = (htData_*) malloc(num_entries * sizeof(htData_));
//point to the pointer that points to the first element in the array
ht.entries = &htDataArray; // WARNING HERE!!!!!!!!!!!!!!!!
return ht_p;
}
I'm trying to copy the **ptr to the struct containing a **ptr.
Update: My simplified code was not accurate so I've posted the actual code.
The problem is that struct htData_ and htData_ are not the same thing! As far as the compiler is concerned, struct htData_ doesn't exist—it's an incomplete type. htData_, on the other hand, is a typedef for an anonymous structure. For a more detailed analysis, see Difference between struct and typedef struct in C++.
So, you're getting a warning because ht.entries is declared as the type struct htData_**, but the right-hand side of that assignment has type <anonymous struct>**. To fix this, you need to define struct htData_:
typedef struct htData_
{
...
} htData_;
This line is not proper:
htData_ array[20] = htDataArray;
You cannot assign a pointer to an array.
In your edited code, here is the problematic line:
//point to the pointer that points to the first element in the array
ht.entries = &htDataArray;
Actually, syntactically it's correct, so it should not give warning. But you are doing wrong stuff here. If you want ht.entries pointing to the first element of array than you need to declare it as,
htData_* entries; // 'struct' keyword not needed ahead of declaration
and assign it as,
ht.entries = &htDataArray[0];

C: pointer to struct in the struct definition

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?

Resources