Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

STRUCTURE POINTER

A pointer of basic or user-defined data type can be member of a structure and even a pointer to a structure can be created.

//Example program to show how a pointer can be a member of a structure

#include

struct data

{

   int  x;  //Stores address of integer Variable

   int *ptr;

};

int main()

{

struct data d1;

d1.ptr=&d1.x;

*d1.ptr=10;

printf(“Value of x inside d1 is %d”,*d1.ptr);

return(0);

}

When a pointer to structure type is created it can store address of any variable of the same structure.  Now the question is how to access the members of structure using pointer.  For this, there are two solutions.

  1. Using * and . operators together
  2. Using arrow(->) or membership operator

//Example program to show how a pointer to a structure can be created

#include

struct data

{

   int  x,y;  //Stores address of integer Variable

};

int main()

{

struct data *sptr,d1;

sptr=&d1;

(*sptr).x=10;

sptr->y=20;

printf(“Value of x inside d1 is %d”,sptr->x);

printf(“\nValue of y inside d1 is %d”,(*sptr).y);

return(0);

}

You cannot copy content of this page