array not storing the address of structure - c

I am trying to implement graph using adjacency list ,according to my knowledge that i have learnt so far if i created variable array pointer to struct adjlistnode of size v*sizeof(struct adjlistnode) thin i can store the addresses of v struct adjlistnode type node in each index of array
Means that each index of array will point to the node of type struct adjlistnode but when i am assigning G->array[i]=NULL it gives me error
||=== Build: Debug in teeest (compiler: GNU GCC Compiler) ===|
C:\Users\Mahi\Desktop\DATA STR\teeest\main.c||In function 'creategraph':|
C:\Users\Mahi\Desktop\DATA STR\teeest\main.c|59|error: incompatible types when assigning to type 'struct adjlistnode' from type 'void *'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
why i am not able to assign NULL to index of array
what should i do if i want to access adjacency list like using G->array[i]=first node address adjacent to ith vertex of graph and later i will add another node if needed
struct adjlistnode{
int dest;
struct adjlistnode* next;
};
struct graph{
int V;
struct adjlistnode* array;
};
struct adjlistnode* getnewnode(int dest){
struct adjlistnode* newnode =(struct adjlistnode*)malloc(sizeof(struct adjlistnode));
newnode->dest=dest;
newnode->next=NULL;
return newnode;
}
struct graph* creategraph(int v){
struct graph* G=(struct graph*)malloc(sizeof(struct graph));
G->V=v;
G->array=(struct adjlistnode*)malloc(v*sizeof(struct adjlistnode));
for(int i=0;i<v;i++){
G->array[i] =NULL;
}
return G;
}

G->array is of type struct adjlistnode *
But
G->array[i] is of type struct adjlistnode.
Thus you cannot assign NULL (of type void *) to G->array[i] of type struct adjlistnode
You should probably have to define array in the struct graph as pointer to pointer
struct graph{
int V;
struct adjlistnode** array;
};
and then the following should work for you
struct graph* creategraph(int v){
struct graph* G=malloc(sizeof(struct graph));
G->V=v;
G->array=malloc(v*sizeof(struct adjlistnode*));
for(int i=0;i<v;i++){
G->array[i] =NULL;
}
return G;
}
** Note1 (as also mentioned by #alk in the comments) that in C, at least since C89 standard, malloc returns void *. void * can be assigned to any other pointer type (and visa versa), thus casting the return value of malloc is not required.
** Note2 (also noted by #alk) that malloc signature is defined with the parameter of type size_t and not int so better to modify the code a little and use the proper type ( Read comparing int with size_t and size_t vs int in C++ and/or C for more info)

array is a (single) pointer to struct adjlistnode. So it can be set to NULL.
G->array = NULL; //is okay
But it is not an array of pointers, so you cannot access the elements of the array and they cannot be set to NULL as well.
For dynamic allocation, you should do this:
struct graph{
int V;
struct adjlistnode** array;
};
struct graph* creategraph(int v){
struct graph* G = malloc(sizeof(struct graph));
G->V = v;
G->array = malloc(v * sizeof(struct adjlistnode*)); //allocation for an array of v pointers
for(int i = 0; i < v; i++){
G->array[i] = NULL;
}
return G;
}
As suggested by #alk, it is better if you pass v as size_t instead of int, as malloc takes size_t.

G->array[i] returns *(array + i * sizeof(struct adjlistnode)) as if array was struct adjlistnode array[].
What you do is store v objects of struct, but you try to initialize them with NULL, like you would a pointer.
What you probably want is
struct graph{
int V;
struct adjlistnode** array;
};
[...]
G->array=(struct adjlistnode**)malloc(v*sizeof(struct adjlistnode*));
That would make array a pointer to an array of pointers.
Then G->array[i] would return a struct adjlistnode* pointer to an object of struct, that you can then initialize with your getnewnode().

Related

Why do I need double pointers?

I don't understand why I need double pointers inside 'struct graph'. Is it because it allows me to access one of the nodes that I made inside the function makeGraph()?
If I use one pointer (struct node *adjList) then I can't set the nodes to NULL that I made inside makeGraph().
I got the code frome programiz.com and in the article that explains this code it says: Don't let the struct node** adjList overwhelm you. All we are saying is we want to store a pointer to struct node*. This is because we don't know how many vertices the graph will have and so we cannot create an array of Linked Lists at compile time.
If I do: graph->adjList[1] does it go to the address of the second node or goes it inside the node? (I'm talking about the nodes that I create inside makeGraph())
I understand the rest of the code. If anyone can help me it would be appreciated.
#include <stdlib.h>
#include <stdio.h>
struct node
{
int vertex;
struct node *next;
};
struct graph
{
int numVertices;
struct node **adjList; // <--- THIS ONE
};
struct graph *makeGraph(int vertices) // Creating a Graph
{
struct graph *graph = malloc(sizeof(struct graph));
graph->numVertices = vertices;
graph->adjList = malloc(sizeof(struct node) * vertices); // creating the nodes
for (int i = 0; i < vertices; i++)
graph->adjList[i] = NULL; // Setting all nodes to NULL
return graph;
}
void addEdge(struct graph *graph, int src, int dest) // Add Edge
{
struct node *newNode = makeNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
struct node *newNode2 = makeNode(src);
newNode2->next = graph->adjList[dest];
graph->adjList[dest] = newNode2;
return;
int main()
{
struct graph *graph1 = makeGraph(4);
addEdge(graph1, 0, 1);
addEdge(graph1, 0, 2);
addEdge(graph1, 0, 3);
}
An adjacency list is being represented as a linked list of struct node. The list is accessed by a pointer to the first element of the list. (The pointer will be NULL when the list is empty.) The pointer has type struct node *.
The graph has a number of vertices as set in the numVertices member of struct graph. Each vertex needs an adjacency list, and each adjacency list needs a struct node *. So the graph needs an array of struct node * of length numVertices. The authors of the code chose to allocate the array dynamically as a separate memory block pointed to by the adjList member. The type of the adjList member is a pointer to the element type. The element type is struct node * so the type of the adjList member is struct node **.
There is another way to allocate the memory for the struct graph and its adjacency list. They could be allocated as single block by changing the adjList member to be a flexible array member, as shown below:
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
struct node
{
int vertex;
struct node *next;
};
struct graph
{
int numVertices;
struct node *adjList[]; // <--- FLEXIBLE ARRAY MEMBER
};
struct graph *makeGraph(int vertices) // Creating a Graph
{
struct graph *graph = malloc(offsetof(struct graph, adjList[vertices]));
graph->numVertices = vertices;
for (int i = 0; i < vertices; i++)
graph->adjList[i] = NULL; // Setting all nodes to NULL
return graph;
}
offsetof(struct graph, adjList[vertices]) is the offset in bytes from the address of a struct graph to the address of the adjList[vertices] array member element. Allocating a block of memory of that size is just large enough to hold a struct graph plus the array of pointers. Another way to specify the size is sizeof(struct graph) + vertices * sizeof(struct node *) or alternatively sizeof(struct graph) + vertices * sizeof(graph->adjList[0]), but I think using the offsetof macro is a more succinct way to specify the size.

Following C code why it is correct to use graph->array[i].head and graph->array[i]->head gives error?

I know that . is used when there is a direct access to a variable in the structure and -> is used when you are accessing a variable of a structure through a pointer to that structure. Also a->b is equivalent to (*a).b
Here we are accessing the pointer variable head of structure AdjList through a pointer stored in graph->array[i] but still using . for access.
graph->array[i].head = *((*graph).array+i).head
graph->array[i]->head = (* *((*graph).array+i)).head
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
struct AdjListNode *head;
};
// A structure to represent a graph. A graph
struct Graph
{
int V;
struct AdjList* array;
};
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph =
(struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of
// array will be V
graph->array =
(struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by
// making head as NULL
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
But when we do tree traversal we do root->left->left = (*(*root).left).left then it works fine.
If anyone can help me understand this with memory diagram that would be a great help, I made this memory diagram for better understanding.

Incompatible pointer types from a pair of data structures in C

Compiling the code below is throwing a warning. Please let me know what's wrong with this code.
warning: assignment from incompatible pointer type [enabled by default] p2->node = p1;
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node1{
int item;
}n1;
typedef struct node2{
struct n1 *node;
}n2;
int main(){
n1 *p1 = malloc(sizeof(n1));
p1->item = 23;
n2 *p2 = malloc(sizeof(p2));
p2->node = p1;
return 0;
}
Type struct n1 does not exist. However you have typedefed a type n1. Therefore you have to use that type in your other "struct".
typedef struct node2
{
n1 *node;
}n2;
Or use the matching struct
typedef struct node2
{
struct node1 *node;
}n2;
You are also not allocating enough memory for the struct in the second malloc call.
The size you give it is size of a pointer sizeof(p2) while it should be either sizeof(*p2), sizeof( struct node2 ) or sizeof( n2 ).

Is it possible to generically free linked lists' memory in C

If I have several linked structures in C like:
struct structA {
int a;
int b;
struct structA *next;
}
struct structB {
char a;
int b;
struct structB *next;
}
and I dynamically allocate memory like this:
struct structA *mystructA = (struct structA*) malloc(sizeof(struct structA));
mystructA->next = (struct structA*) malloc(sizeof(struct structA));
struct structB *mystructB = (struct structB*) malloc(sizeof(struct structB));
mystructB->next = (struct structB*) malloc(sizeof(struct structB));
do I always have to free it for each struct type like this:
struct structA *p, *pNext;
for (p = mystructA; p != NULL; p = pNext) {
pNext = p->next;
free(p);
}
struct structB *p, *pNext;
for (p = mystructB; p != NULL; p = pNext) {
pNext = p->next;
free(p);
}
or is there any generic solution? I assume there is no other solution because the free() procedure must know how many bytes have to be freed. But maybe I'm wrong and someone can teach me better.
The standard way is to make the "list part" the first element of the structure, and let each derived struct share this same prefix. Since the first element is guaranteed to be placed at offset zero this wil work.
Example snippet:
#include <stdlib.h>
#include <string.h>
struct list {
struct list *next;
};
struct structA {
struct list list;
int a;
int b;
};
struct structB {
struct list list;
char a;
int b;
};
void *create_any(size_t size)
{
struct list *this;
this = malloc (size);
if (!this) return this;
memset(this, 0, size);
this->next = NULL;
return this;
}
void free_all_any(struct list **lp) {
struct list *tmp;
while ((tmp = *lp)) { *lp = tmp->next; free(tmp); }
}
#define CREATE_A() create_any(sizeof(struct structA))
#define CREATE_B() create_any(sizeof(struct structB))
#define FREE_A(pp) free_any((struct list **) pp)
#define FREE_B(pp) free_any((struct list **) pp)
int main(void)
{
struct structA *ap;
struct structB *bp;
ap = CREATE_A ();
bp = CREATE_B ();
// some code here ...
FREE_A( &ap);
FREE_B( &bp);
return 0;
}
This is more or less the method used in the linux kernel, but a lot more preprocessor magic is used there. (and there is no malloc there, obviously)
Since free() accepts pointers to void * and structA and structB both have the same size, you can pass both pointer types.
This is, however, not optimal in terms of elegance. You should think about the following questions:
Why do you have two different structs with the same members?
Why do you not have a generic list item type, such as the following:
struct list_node {
void *data;
struct list_node *next;
}
Actually, this is a very interesting question. The part is true that you have to free() each struct type individually, as they have been malloc()-ed individually, and each memory block has been allocated specifically for that type.Also, on some systems char and int have different storage sizes, but you can try a solution like Phillip provided. For more info, read about the doom memory engine. On a side note, please don't cast malloc() in C. The funny thing is that once the program is terminated, the operating system will reclaim the memory, so if you only deallocate the structures near the end of the program, when you don't need them anymore, it may not be necessary to free() them

Store address dynamic array in c

I'm trying to save the address of a dynamic array index. The last line of this function is what gives the pointer error.
static struct sstor *dlist
struct node *ins_llist(char *data, struct llist *l) {
struct node *p, *q;
q = malloc((size_t)sizeof(struct node));
if(q == NULL)
return(NULL);
if(ins_list(data, &dlist))
return NULL;
q->item = &(dlist->item[(dlist->sz)-1]); // Problem?
...}
Allocation of dlist
struct llist *init_llist(void) {
struct llist *l;
dlist = init_list(INITSZ);
if(dlist == NULL)
return(NULL);
This is my node
struct node {
char **item;
struct node *next;
struct node *prev;
};
This is my array
struct sstor {
int sz;
int maxsz;
char item[][1024];
};
I'm still new to pointers. The line below gives the error: assignment from incompatible pointer type
q->item = &(dlist->item[(dlist->sz)-1]);
Presuming that you allocate an actual struct node for q to point to...
The "incompatible pointer types" error arises because q->item has type char ** (a pointer to a pointer to char), and &(dlist->item[...]) has type char (*)[1024] (a pointer to an array of 1024 char). These types simply aren't compatible (there is no actual "pointer to char" object for q->item to point to).
You can fix the problem in two ways. The first is by changing the declaration of struct node to this:
struct node {
char (*item)[1024];
struct node *next;
struct node *prev;
};
The second is by changing both the declaration of struct node to this:
struct node {
char *item;
struct node *next;
struct node *prev;
};
..and changing the assignment statement to this:
q->item = dlist->item[(dlist->sz)-1]; // No Problem!
(The first alternative makes q->item point to the entire array, and the second makes it point to the first char in the array. The perspecacious will note that these are pointers to the same location, but with different types. Usually, the second form is what you want).
char** is not the same as char[][1024]
Try to fix the sstor structure like this:
struct sstor {
int sz;
int maxsz;
char *item[1024];
};

Resources