what is the meaning of a double pointer of struct in C - c

I have a problem understanding this structure , would love to get a clear explanation.
typedef struct exp{
int x;
struct exp *parent;
struct exp **children;
}
what does it mean the parent and the children ?
"parent" is an array of this structure?
and what is the meaning of the children ? it's an array of arrays?!
I really can't understand..
and last thing, If I'm adding an element , it becomes a specific child of some parent, how can I reach all the children of a parent? shouldn't it be a structure "List" (using next etc .. ? )?
thank you!!

This image shows a possible scenario:
It was obtained using this code and DDD
#include <stdio.h>
#include <stdlib.h>
struct exp{
int x;
struct exp *parent;
struct exp **children;
};
int main ()
{
struct exp *x = calloc(1, sizeof(x[0]));
x->x = 42;
x->parent = calloc(1, sizeof(x[0]));
x->children = calloc(5, sizeof(x->children[0]));
x->children[0] = calloc(1, sizeof(x[0]));
x->children[2] = calloc(1, sizeof(x[0]));
x->children[3] = calloc(1, sizeof(x[0]));
x->children[4] = calloc(1, sizeof(x[0]));
return 0;
}
Basically, the children field is a vector of pointers to struct exp. You decide how many elements to put there and the other things.
PS: Code is only a demo, it has not quite a good quality.

It's a pointer to a pointer, which in that case seems to be used as a list of struct exp.
Each struct exp has a reference to it's "parent", and a pointer to a list of children struct exp.
typedef struct exp{
int x;
struct exp *parent;
struct exp **children;
} element;
// create ROOT elemnt
element * root = (element*) malloc(sizeof(element)); //alocate mem. for 1 element
Once we have a "root" we can add children, following is pseudo-code
for 1 to 10{
child = new element;
child->parent = root; // tell the child who is his parent
addToRoot( root , child); // call a function that inserts elemnts to root
}
So now we should have root with a list of 10 elements:
_______________ _______________
| | (children) | | - (parent) points to struct exp, root
| root | - points to list of struct exp -> | child 0 |
| | | | - (children) points to null; // if it's empty
_______________ _______________
_______________
| | - (parent) points to struct exp, root
| child 1 |
| | - (children) points to null; // if it's empty
_______________
_______________
| | - (parent) points to struct exp, root
| child 2 |
| | - (children) points to null; // if it's empty
_______________
_______________
| | - (parent) points to struct exp, root
| child 3 |
| | - (children) points to null; // if it's empty
_______________
.
.
.
_______________
| | - (parent) points to struct exp, root
| child 9 |
| | - (children) points to null; // if it's empty
_______________
Something like that... Did it help?

It's an array of pointers to child nodes- this looks like some kind of tree structure.
There is a parent node for each node, and each node has one or more children. You can travel from a parent to one of its children with
expInstance->children[i];
Where i is a number indicating one of the child nodes. It's not really clear how many child nodes there are from this definition- it could be one, or two, or a million. But given that information you could loop through them. Either with
for(i=0; i<NUMBER_OF_CHILDREN_NODES;i++){
expInstance->children[i];
}
if you know the length of the array in advance or the somewhat strange
while(expInstance.children[i++]){
expInstance->children[i];
}
(There are a couple variously clever ways to do this, but there has to be the assumption built into the structure that there will be a null pointer in the last slot of this array in order to terminate it.)

It is hard to say without seeing more code. Are you trying to implement a tree? From this definition, this is what apparent to me:
parent points to the parent node of this structure.
children points to the array of pointers of all its child nodes.

First of all, remember that a structure cannot contain an instance of itself. IOW, you cannot do something like
struct exp {
int x;
struct exp parent;
...
};
The main reason for this is that the struct type is not complete at the point where you try to declare the parent member; since you haven't finished describing the type yet, the compiler doesn't know how big the parent member should be. Not to mention that the parent member would itself have a parent member of type struct exp, which would in turn have a parent member of type struct exp, which would in turn have a parent member of type struct exp, etc., etc., etc., etc. You'd wind up with an object that required an infinite amount of storage.
So you cannot have a member of type struct exp. However, you can have a pointer to an object of type struct exp; you can create pointers to incomplete types, and all struct pointer types have the same size and representation (IOW, struct foo *pf and struct bar *pb will have the same size, even if struct foo and struct bar do not).
So the parent member is (probably) only meant to point to a single instance of type struct exp (each instance of struct exp has one parent). Likewise the children member is (probably) meant to be a single-dimensioned array of pointers to struct exp (each instance of atruct exp can have zero, one, or more children).

Related

what is the difference between *root and **root?

I was iterating a tree data structure which has a pointer to its root as follows-
struct node *root;
when I have to pass reference of this root as a parameter to a function.... I have to pass it like-
calcHeight(&root);
-
-
-
//somewhere
int calcHeight(struct node **root) // function defination is this
My question is- why do we need to pass "root" pointer as &root? can't we just pass root like--
struct node *root;
calcHeight(root);
int calcHeight(struct node *root);
// EDIT
void someFunct(int *arr){
printf("arr2 inside someFunct is %d\n",arr[2]);
arr[2]=30;
}
int main()
{
int *arr=(int*)calloc(10,sizeof(int));
printf("arr[2] is %d\n",arr[2]);
someFunct(arr);
printf("arr[2] finally is %d\n",arr[2]);
return 0;
}
In this case arr in main function is modified even when I'm not passing the address of arr.
I'm getting the fact that for structures and single value vars we HAVE to pass the address like someFunct(&var) but this is not necessary for arrays? for arrays we write someFunct(arr)
But I'm not getting the reason behind this?
struct node * is a pointer to a struct node.
struct node ** is a pointer to a pointer to a struct node.
The reason for passing in a struct node ** could be that the function needs to modify what the struct node * is actually pointing at - which seems odd for a function named calcHeight. Had it been freeNode it could have made sense. Example:
void freeNode(struct node **headp) {
free(*headp);
*headp = NULL; // make the struct node * passed in point at NULL
}
Demo
Another reason could be to make the interface consistent so that one always needs to supply a struct node ** to all functions in the functions supporting struct nodes - not only those actually needing to change what the struct node * is pointing at.
Regarding the added // EDIT part:
In this scenario there is no reason to send in a pointer-to-pointer. If you do not need to change the actual pointer, you only need to send in the value of the pointer.
Example memory layout:
Address What's stored there
+-----+
| +0 | uint64_t ui1 = 1 <--+
+-----+ |
| +8 | uint64_t ui2 = 2 |
+-----+ |
| +16 | uint64_t* p = &ui1 ---+
+-----+
Now, if a function only need an uint64_t value, you can send in ui1, ui2 or *p to that function.
void someFunc(uint64_t val) { ++val; ... }
The changes this function makes to val are not visible to the caller of the function.
If a function is supposed to be able to make changes that are visible to the caller of the function, send in a pointer:
void someFunc(uint64_t *valp) { *valp = 10; }
Calling it with someFunc(&ui1); or someFunc(p); will change ui1 and assign 10 to it.
If you have a pointer and want to change what it's actually pointing at, which is what your original question was asking, you would need to send in a pointer to that pointer:
void someFunc(uint64_t **valpp) { *valpp = &ui2 }`
If you call that with someFunc(&p) (where p is currently pointing at ui1) you will find that after the function call, p will point at ui2:
+-----+
| +0 | uint64_t ui1 = 1
+-----+
| +8 | uint64_t ui2 = 2 <--+
+-----+ |
| +16 | uint64_t* p = &ui2 ---+
+-----+
Because in calcHeight you're passing your argument by value. If you want to modify the pointed value by root you need to pass the adress of the pointer.
First one is a pointer to node which is a structure.
struct node *root;
defines root as a variable which can store the address of a node.
Second one is a pointer to a pointer to node which is a structure.
struct node **root;
defines root as variable which can store address of another variable which has the address of a node.
why do we need to pass "root" pointer as &root?
calcHeight(&root);
C passes arguments by value, not by reference. so, you have to pass the address of root to modify the value of root.

Pointers in Structure pointing to another strucutre

I am trying to understand how pointers in linked lists work. So far i am having lots of trouble trying to figure out where the pointer is pointing to and how a pointer of a type struct works( I know we need to allocate memory from the heap but can't quite understand it, but maybe that's a different question altogether).
Lets take this structure:
typedef struct Node {
int data;
struct Node *link;
} Node;
What I think will happen now is:
Say you have a pointer of type Node in the main function, Node* p and this is allocated memory (using malloc).
Now if we have some data p->data=5; , p points to the beginning of this data (at least this is what i think is happening).
Where exactly does link point to?
So now, i come across this particular piece of code:
typedef struct Node {
int data;
struct Node *link;
} Node;
typedef struct List {
Node* head;
int number_of_nodes;
} List;
So this is complete chaos in my brain! .
Now in the structure List, what is head doing? What is it pointing to? And how would you create a linked list at all with these two lists??
I am really trying my level best to understand how linked lists work, but all the pointers make it too hard to keep track of. You might suggest i start with something simple and i did, and i have already mentioned how much i understand. But the head pointer in the second structure has completely thrown me off track!
It would make my life so much more easier if someone could help me explain it while keeping track of the pointers.
Where exactly does link point to?
link points to another object of the same type:
+------+------+ +------+------+ +------+------+
| data | link |---->| data | link |---->| data | link | ----> ...
+------+------+ +------+------+ +------+------+
Now in the structure List, what is head doing? What is it pointing to?
head points to the first node in a list:
+-----------------+ +------+------+ +------+------+
| head |---->| data | link |---->| data | link |----> ...
+-----------------+ +------+------+ +------+------+
| number_of_nodes |
+-----------------+
I am really trying my level best to understand how linked lists work,
Don't feel bad - linked lists threw me for a loop in my Data Structures class (my first "hard" CS class). It took me a solid week longer than my classmates to grok the concept. Hopefully the pictures help.
Edit
what happens if you have a pointer to the structure List, memory allocated and all? Where does it point to then (according to the diagrams, which did help by the way)
So, let's assume you have the following code:
/**
* Create a new list object. head is initially NULL,
* number_of_nodes initially 0.
*/
List *newList( void )
{
List *l = malloc( sizeof *l );
if ( l )
{
l->head = NULL;
l->number_of_nodes = 0;
}
return l;
}
int main( void )
{
List *l = newList();
...
}
Then your picture looks like this:
+---------+ +--------------------+
| l: addr | ----> | head: NULL |
+---------+ +--------------------+
| number_of_nodes: 0 |
+--------------------+
(addr represents some arbitrary memory address)
Now let's say you add a node to your list:
/**
* Create a new node object, using the input data
* link is initially NULL
*/
Node *newNode( int data )
{
Node *n = malloc( sizeof *n );
if ( n )
{
n->data = data;
n->link = NULL;
}
return n;
}
void insertNode( List *l, int data )
{
Node *n = newNode( data );
if ( n )
{
/**
* If list is initially empty, make this new node the head
* of the list. Otherwise, add the new node to the end of the
* list.
*/
if ( !l->head ) // or n->head == NULL
{
l->head = n;
}
else
{
/**
* cur initially points to the first element in the list.
* While the current element has a non-NULL link, follow
* that link.
*/
for ( Node *cur = l->head; cur->link != NULL; cur = cur->link )
; // empty loop body
cur->link = n;
}
l->number_of_nodes++;
}
}
int main( void )
{
List *l = newList();
insertNode( l, 5 );
...
}
Now your picture looks like this:
+---------+ +--------------------+ +------------+
| l: addr | ----> | head: addr | ---> | data: 5 |
+---------+ +--------------------+ +------------+
| number_of_nodes: 1 | | link: NULL |
+--------------------+ +------------+
You could add another node:
int main( void )
{
List *l = newList();
insertNode( l, 5 );
insertNode( l, 3 );
...
}
then your picture becomes
+---------+ +--------------------+ +------------+ +------------+
| l: addr | ----> | head: addr | ---> | data: 5 | +--> | data: 3 |
+---------+ +--------------------+ +------------+ | +------------+
| number_of_nodes: 2 | | link: addr | --+ | link: NULL |
+--------------------+ +------------+ +------------+
Naturally, you'd want to add some error checking and messages in case a node couldn't be allocated (it happens). And you'd probably want an ordered list, where elements are inserted in order (ascending, descending, whatever). But this should give you a flavor of how to build lists.
You'd also need functions to remove items and free that memory. Here's how I'd free an entire list:
void freeList( List *l )
{
Node *prev, *cur = l->head;
while( cur && cur->link )
{
prev = cur;
cur = cur->link;
free( prev );
}
free( cur );
}
int main( void )
{
List *l = newList();
...
freeList( l );
free( l );
...
}
… a pointer of a type struct…
A pointer cannot be of a type struct. A pointer can point to a structure.
C has objects. Objects include char, int, double, structures, and other things. A structure is a collection of objects grouped together.
In main, if you define p with Node *p;, you then have a pointer p. It has no value because you have not given it a value. When you execute p = malloc(sizeof *p);, you request enough memory for the size of the thing p points to (*p). If malloc returns a non-null pointer, then p points to a Node structure.
Then p->data refers to the data member of that structure. p->data is shorthand for (*p).data, in which *p means “the object p points to” and .data means “the data member in that object.”
After p = malloc(sizeof *p); and p->data = 5;, p->link does not point to anything because you have not assigned it a value. In a linked list, you would use malloc to get memory for another Node, and then you would set the p->link in one Node to point to the new Node. In each Node, its link member points to the next Node in the list. Except, in the last Node, p->link is set to a null pointer to indicate it is the last Node.
In List, you would set head to point to the first Node in a list of Node objects.

C Confusion about pointer to pointers memory allocation?

I apologize if this might be viewed as a duplicate, but I cannot seem to find a conclusive answer that satisfies my question.
So I have a struct with a self referential pointer to pointers.
struct Node {
int id;
int edge_count;
struct Node **edges;
}
static struct Node s_graph[MAX_ID+1];
I then have a function that allocates some memory.
int add_edge(int tail, int head)
{
struct Node *ptail, *phead;
ptail = &s_graph[tail];
phead = &s_graph[head];
ptail->edges = realloc(ptail->edges, ++ptail->edge_count * sizeof(struct Node *));
if (ptail->edges) {
*(ptail->edges + ptail->edge_count - 1) = phead;
return 0;
}
return -1;
}
The above seems to work just fine. However, I keep seeing posts about pointer to pointers that lead me to wonder if I need to do something like the following in add_edge:
struct Node *phead = malloc(sizeof(struct Node *));
However, this does not seem logical. There should be enough memory for ptail->edges to store this pointer after the realloc call. I am fairly confident that I did the allocation correctly (albeit, inefficiently), but it is kind of sending me on a mind trip ... So when people declare pointer to pointers (e.g., **ptr) and then allocate memory for both ptr and *ptr, wouldn't that technically make ptr a pointer to pointers to pointers (and maybe clearer to declare as ***ptr)? Or maybe I am wrong and missing something conceptually?
Thank you in advance!
It depends on the situation, there is no general answer. If you have a pointer to pointer, eg Node**, and you want to store new data into it, then you need to have two levels of allocations, otherwise one is enough.
struct Node** nodes = calloc(AMOUNT, sizeof(struct Node*));
Now you have an array of struct Node* elements, so each element is a pointer to a struct Node.
Now how do you fill this array? You could want to insert new nodes inside it. Then you wouold require to allocate them, eg
nodes[0] = calloc(1, sizeof(struct Node)); // <- mind Node, not Node*
But in your situation you just want to set the address to an element of an array of the static variable s_graph, so you don't need to allocate a second level, you directly set the value.
So:
struct Node** nodes = calloc(AMOUNT, sizeof(struct Node*));
nodes -> | 0 | 1 | 2 | 3 |
nodes[0] = calloc(1, sizeof(struct Node))
nodes -> | 0 | 1 | 2 | 3 |
|
|
v
| NODE |
But if you have s_graph you already have them allocated, so it's something like:
static struct Node s_graph[MAX_ID+1];
struct Node** nodes = calloc(AMOUNT, sizeof(struct Node*));
nodes -> | 0 | 1 | 2 | 3 |
s_graph -> | N1 | N2 | N3 |
nodes[0] = &s_graph[0];
nodes -> | 0 | 1 | 2 | 3 |
|
|----|
v
s_graph -> | N1 | N2 | N3 |

what is the purpose of adding "struct name *next" inside "struct name" in linked list. I mean i actually cant process what exactly happens

#include<stdio.h>
#include<stdlib.h>
typedef struct employ
{
int reg;
int sal;
char *name;
struct employ *next; // I want to know the purpose of this line
}EMP;
int main(void)
{
EMP *first,*emp1,*emp2,*emp3,*ans;
first = (EMP *)malloc(sizeof(EMP));
first->next = NULL;
/////first///////
emp1 = (EMP *)malloc(sizeof(EMP));
if(emp1 == NULL)
{
perror("malloc error");
exit(1);
}
emp1->reg = 100;
emp1->sal = 30000;
emp1->name = "james";
emp1->next = first;
first = emp1;
/* I havent completed the program bcoz its not necessary here */
In a linked list, each node contains a pointer to the next node.
struct employ *next;
is used to achieve that effect
+---------+ +---------+
| Node1 |------->| Node2 |----> ...
+---------+ +---------+
if you are in node1, next is a pointer to node2, that way you can accesss next element from the current element
A linked list is a data structure consisting of a group of nodes which together represent a sequence. Here the reg, sal and *name represent your data structure and *next will hold the link to next element in the list.
Consider the following data structure
struct sample {
int number;
struct sample *next;
};
The pictorial representation for this structure would be
The above example has 3 elements, every element is an individual structure variable. In the above example the variable 1 has members number and *next. Here, the number contains 12 and the *next contains the address of the structure variable holding the data 99. Since the type of the variable holding the data 99 is of type struct sample that is why we take *next type as struct sample type.

Structure In C for linked list

Sorry for asking such a stupid question but I am really confused.
struct Amit
{
int a;
struct Amit *link;
}
*start;
Here both *link and *start are used to point to a node of a linked list, but what's the difference between these two and why can't we put *start inside the structure body?
The link is a member of the structure type. Every structure of type struct Amit has one.
The start is a variable of type 'pointer to struct Amit'. At any given time, there can be at most one variable called start visible.
You could put start inside the structure, but it would become a member of the structure (like link), and you would still need to declare variables of the structure type, or pointers to them.
The idea is that each structure on a list except the last contains a link pointer to the next structure on the list. Normally, the last structure on the list has a link pointer that is NULL (0). When searching down a list, you look at the values, and when you need the next item, you follow the link to it, stopping when the link is NULL.
struct Amit *item = start;
while (item != NULL && item->a != value_wanted)
item = item->link;
It is possible to build a circular linked list instead, which has a different stop criterion.
Looking at the comments, and explaining a bit more...
One way to create a list is:
struct Amit root = { 0, NULL };
struct Amit *start = &root;
The variable root is a structure initialized with root.a == 0 and root.link == NULL (or, equivalently, root.link == 0). The pointer variable start points to (stores the address of) root. Given a new node:
struct Amit next = { 1, NULL };
we can add that to the front of the list which start points to:
next.link = start;
start = &next;
A more plausible way to create a list is by dynamically allocating nodes, including the root node. Consistency is crucial because you have to free the dynamically allocated nodes, and having some nodes dynamically allocated and others not is messy. (I'm assuming that function void *emalloc(size_t nbytes); is a cover function for malloc() that never returns a null pointer - so it does the error checking for me.)
// Create the empty list
start = emalloc(sizeof(*start));
start->a = 0;
start->link = NULL;
// Create a node
struct Amit *node = emalloc(sizeof(*node));
node->a = 42;
node->link = NULL:
// Add the node to the font of the list
node->link = start;
start = node;
You'd normally package this stuff up into functions which manage the allocation, initialization and linking of the nodes.
struct Amit *add_node(struct Amit *start, int value)
{
struct Amit *node = emalloc(sizeof(*node));
node->a = value;
node->link = start;
return start;
}
start = add_node(start, 42);
start = add_node(start, 30);
start = add_node(start, 18);
for (node = start; node->link != 0; node = node->link)
printf("Node: %d (%p)\n", node->a, node->link);
Etc.
This basically defines three things:
a struct (don't capitalize it as Struct, by the way)
a member variable within the struct, named link
a variable outside the struct named start
You can reduce the confusion by separating the definition of the struct from the declaration of the start variable, like this:
struct Amit
{
int a;
struct Amit *link;
};
struct Amit *start;
If you rename "link" to "next" it might help you get a better sense of it. A linked list is like a chain - your "start" (or as usually called, the list "head") is the first ring of the chain, and the next ring of the chain is linked to it through your "next" pointer (in your case, your "link" pointer). You know you got to the last item on your chain when there are no other rings (link is NULL).
Start points to the top of the list and is available globally to your program. Whereas link just keeps track of the next item, and is available when referring to a specific 'node'. See this diagram it may help you understand with a visual!
link internally tracks the following item which keeps track of where the next component is as it is not necessarily contiguous the way arrays are.
+------+ +------+ +------+
| data | | data | | data |
+------+ +------+ +------+
| link |---->| link |---->| link |----> NULL
+------+ +------+ +------+
^
|
START (Keep track of the whole list.)
Hope that helps clarify.

Resources