Hello all,
I am implementing linked list programs in C. And for that i want to know more about Self Referential Structure in C. If anyone knows more detail information about it then please tell me.

I am having one example here which helps you to understand more clearly what i want to mean.

struct list
{
  int no;
  struct list *next;
};

Please help me on the same.

Recommended Answers

All 7 Replies

Compound literals
C99 added compound literals, is based on the brace-enclosed initializer syntax. The motivation for adding this feature to C99 was its notational conciseness, convenience, and usefulness, rather than an abstract desire to make struct a first class type.
Sample - 1

#include <stdio.h>
// compound literals.
struct data
{
   int a;
   float b;
};
int main()
{
  struct data t={10,10.20f};
  t=(struct data){30};
  printf("\n%d  %f",t.a,t.b);  // 30  0.000000
  t=(struct data){.b=22.22f};  
  printf("\n%d  %f",t.a,t.b);   // 0  22.219999
 return 0;
}

Sample - 2

#include <stdio.h>
// compound literals.
struct data{
   int a;
   struct data *ptr;
};
int main()
{
  struct data p=(struct data){.a=10,.ptr=&(struct data){.a=20,.ptr=NULL}};
  printf("\n%d  %d",p.a,p.ptr->a);
 return 0;
}

Sample - 3

#include <stdio.h>

int main()
{
 int *p;
 p=(int []){22,33,44,55};
 return 0;
}

Links
Draft of the C99 standard with corrigenda TC1, TC2, and TC3 included
http://en.wikipedia.org/wiki/C99

your structure is linked list
each element in the linked list have pointer to the next element in the list

struct list
{
	int value;
	struct list *next;
};

struct list *first = (struct list *)malloc(sizeof(struct list));	//pointer to the first element in the list
first = first->next;		//set the pointer to the second element in the list, using pointer in first node

//------------------------------------
struct list *firstNode = (struct list *)malloc(sizeof(struct list));
struct list *secondNode = firstNode->next;
struct list *thirdNode = secondNode->next; //or firstNode->next->next
...

I never liked the term "Self Referential Structure" because it implies that objects of the structure always refer to themselves. A self referential structure is better called a recursive structure. If the pointer is not NULL, it points to another instance of the structure. The null pointer is a base case for recursion. You can print a list recursively as an example of the relationship between self referential structures and recursion:

#include <stdio.h>

typedef struct Node
{
    int item;
    struct Node *next;
} Node;

void RecursiveTraverse(Node const* head);

int main()
{
    /* manually define a list for the example */
    Node listBase[] = 
    {
        {0, NULL},
        {1, NULL},
        {2, NULL},
        {3, NULL},
    };
    size_t const sz = sizeof listBase / sizeof *listBase;

    {
        /* set all links but the last node */
        size_t x;
        for (x = 0; x < sz - 1; ++x)
        {
            listBase[x].next = &listBase[x + 1];
        }
    }

    /* run example of recursive structure */
    RecursiveTraverse(&listBase[0]);

    return 0;
}

void RecursiveTraverse(Node const* head)
{
    /* null pointer is the base case */
    if (!head) return;

    printf("%d\n", head->item);
    RecursiveTraverse(head->next);
}

If the pointer points to the same object, it is a circular reference that represents infinite recursion because there is no base case:

Node circular = {0, NULL};

circular.next = &circular; /* now a circular reference */

Circular references are usually a bad thing unless you have some logic in place to resolve it into a base case. Ring buffers are a good example of that kind of resolution, if you are interested in learning more.

here list is a structure
having data of integer type with variable "no"
and a pointer which is refering to structure of same type list
hence called self referential structure.......

Ummmm self refrential structure is basically nothing but referring to a structure of its own type.

struct node {
    int data;
    struct node *next;
};

In the code above , a pointer is used . Pointer is used for storing either a valid address or NULL.
here next is a pointer which will store some address which is of struct node type .
for eg.
in the code:-

int a=5;
int *b;
b=&a;

b is a pointer which will store the address of any integer data.
similarly with *next , it will store the adress of that element whose memory block have an integer space and a space for storing of address of struct node type.

hey dont make negative reputation or voting, I didn't see the post is 3 yrs old, from the next time I will sure keep this thing in mind.

    /*  self referenial structure is nothing but pointing to the same type, in more detial *Pointing to itself* rather rather than same type,  */

  struct think {

  ...
  struct think *somepointer; /* pointing to itself "think type structure" i.e.. self referential structure */
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.