#include <stdio.h>
#include <stdlib.h>
str1
{
int k;
struct str1 * ptr1;
};
int main(void)
{
int * p1, * p2;
struct str1 * ptr2;
ptr2 = (struct str1 *) malloc(sizeof(struct str1));
ptr2 -> ptr1 = (struct str1 *) malloc(sizeof(struct str1));
ptr2 -> ptr1 -> ptr1 = ptr2;
ptr2 -> k = 7;
ptr2 -> ptr1 -> k = 25;
p1 = (int *) ptr2 -> ptr1;
p2 = (int *) ptr2 -> ptr1 -> ptr1;
printf("%d %d\n", * p1, * p2);
ptr2 -> ptr1 -> ptr1 -> k ;
printf("%d %d\n", * p1, * p2);
ptr2 -> ptr1 -> ptr1 = ptr2 -> ptr1;
ptr2 -> ptr1 -> ptr1 -> k = 44;
printf("%d %d\n", * p1, * p2);
* p1 = * p2 = 65;
if( ptr2 = = ptr2 -> ptr1 ) printf("Equal\n");
else printf("Not equal\n");
return 0;
}
In this code, I did not understand what this ptr2 -> ptr1 -> ptr1 = ptr2;
code works. Is it attaching a structure another structure and ptr2 -> ptr1 -> ptr1 = ptr2;
What ths code pointing at. Sorry for my bad english. Thank you for your answers.
CodePudding user response:
The str1
struct
contains a pointer to another structure of type str1
. So it is a recursive function, and you can dereference infinitely (or until you reach a str1
struct
that has its pointer equal to NULL.
So ptr2 -> ptr1 -> ptr1 = ptr2;
is assigning the current structure of ptr2
to (recursively going up to the last) the last structure. At that point that structure's own pointer is NULL, so you cannot dereference further.
CodePudding user response:
First of all the the structure str1
declaration is invalid as it misses the keyword struct
which introduces a structure declaration. I am assuming it's a typo. The other typo is in equal to operator in this statement
if( ptr2 = = ptr2 -> ptr1 ) printf("Equal\n");
^^^
it should be ==
.
The structure str1
is a self-referential1) structure as the ptr1
member is pointer to itself i.e. struct str1
.
This statement
ptr2 = (struct str1 *) malloc(sizeof(struct str1));
allocates memory for struct str1
type (assuming malloc
is success) and the pointer returned will be assigned to ptr2
.
The in-memory view would be something like this:
ptr2---
|
-------
| | |
-------
k ptr1
The next statement
ptr2 -> ptr1 = (struct str1 *) malloc(sizeof(struct str1));
does same as what its previous statement does i.e. allocates memory for struct str1
type. The allocated memory reference will be assigned to pointer ptr2 -> ptr1
.
The in-memory view would be something like this:
ptr2---
|
------- -------
| | |--------->| | |
------- -------
k ptr1 k ptr1
The next statement
ptr2 -> ptr1 -> ptr1 = ptr2;
will assign the pointer ptr2
to ptr2 -> ptr1 -> ptr1
. That means, after execution of this statement, both pointers ptr2
and ptr2 -> ptr1 -> ptr1
will point to same memory address and a loop will be created.
The in-memory view would be:
ptr2--- -----------------------------
| | |
------- ------- |
| | |--------->| | |----
------- -------
k ptr1 k ptr1
Note that this statement
ptr2 -> ptr1 -> ptr1 = ptr2 -> ptr1;
will make pointer ptr2 -> ptr1 -> ptr1
point to structure which it is member of. The in-memory view would be:
ptr2--- -----------
| | |
------- ------- |
| | |--------->| | |----
------- -------
k ptr1 k ptr1
Hope this clarifies your doubt. Let me know if you have any further question or confusion.
1). A self-referential structure is a structure in which one of its members is a pointer to the structure itself. It's commonly used in data structures like Linked List, Tree etc. implementation.