Pointer To Structure

  • As Pointer are the variable which stores the address of other variable.
  • So , Now we will learn how store the address of structure variable.
  • As you know pointer can point to a variable,only if their data type are same.
  • For example:int type of pointer can only point to int type of variable or array,etc
  • Because of that ,for pointing a structure you will need to make a pointer of structure datatype. So now we will learn how to do all that things
  • So , let start doing it.
  • for learning that we will define a structure student as name , rollno ,div as it's member.

Definition:
struct student  //structure name
{
    int  rollno; //member1 ,integer variable which will store,
                 //student roll no.
                 
    char  name[20]; //member2 a character array of 20 character
                    //which will store name of student.
                    
    char  div;  //member3 a character variable,
              //which will store division of student.
};

As we have define a structure now we will declare a pointer of structure student data type.

Syntax:declaring pointer of structure student data type
struct structure_name  *pointer_variable_name;

Declaring:pointer of structure student datatype.
struct  student  *s;

Let's see memory view of structure pointer,by looking it you will get an great understanding of pointer of structure variable



Now we will access the member of pointer of structure variable

Syntax:Accessing the member
pointer_variable_name  -> member_name;
  • We use arrow operator for accessing the member of structure, when structure is point by a pointer.
  • -> Arrow operator.

Accessing the members:
//accessing rollno
s -> rollno;


//Accessing Name

s -> name;


//Accessing div

s -> div

Now we will take members as input from the user.
void main()
{
int i;
//declaration of pointer variable of
//structure student data type.
struct student *s,s1;
s = &s1;
//Assigning address of struct 
//variable s1 to pointer variable s


//Taking student roll no as input
printf("\nEnter the roll no of student:");
scanf("%d",&s->rollno);

//Taking student name as input
printf("\nEnter the name of student:");
scanf("%s",s->name);

//Taking student Division as input
printf("\nEnter the student div:");
(s->div) = getche();


//Printing the values which is taking from the user. 
printf("student roll no and name is %d and %s and studies in %c Division",s->rollno,s->name,s->div);
    
    }

Output:
Enter the roll no of student: 1
Enter the name of student: Roy
Enter the student div:A

student roll no and name is 1 and Roy and studies in A Division.


Dynamic memory allocation 

  • Sometimes, the number of structure variables you declared may be insufficient. You may need to allocate memory during run-time. Here's how you can achieve this in C programming

In this program we will use malloc() function

Syntax:
pointer_name  =  (structure _name  *)   malloc(sizeof(structure_name)); 

Allocating memory dynamically:to pointer of student structure datatype
void main()
{
//declaration of pointer variable of
//structure student data type.
struct student *s;
s = (struct student*)malloc(sizeof(struct student)*1);
//allocating memory dynamically using malloc()
}

Let's take input from the user
printf("\nEnter the roll no of student:");
scanf("%d",&s->rollno);

//Taking student name as input
printf("\nEnter the name of student:");
scanf("%s",s->name);

//Taking student Division as input
printf("\nEnter the student div:");
(s->div)=getchar();

Let's print the data on the screen
//printing rollno
printf("\n%d",s->rollno);
//printing name
printf("\n%s\N",s->name);
//printing div
printf("\n%c",s->div);

  • This how we can point to a structure variable dynamically.
  • So ,do more practice on pointers and structures for understanding the concept more better.








Further Topics:


People Also Searched: