Self referential structure

 /*

we can change the value of variables by using pointer in two different

ways first by pointer derefernce line no. 19

and second by line no 21*/


#include <stdio.h>


int main(){

    struct chapter{

    char name [24];

    int page;

    struct chapter *nextchapter;

    };



struct chapter ch1 = {"introduction of programming" , 12, NULL};

struct chapter ch2 = {"loop" , 15 , NULL};

struct chapter ch3 = {"break statement", 8 , NULL};


ch1.nextchapter = &ch2;

ch2.nextchapter = &ch3;

struct chapter *c = &ch1;

while( c != NULL) {

     printf("title : %s , pages = %d\n" , (*c).name , (*c).page);

     c = (*c).nextchapter;

}


 return 0;

}

Comments

Popular posts from this blog

Factors : using C

Cross shaped pattern

Factorial ! calculation