Give the clarity on Pointer structures with suitable example in C language

CServer Side ProgrammingProgramming

Pointer to structure holds the address of an entire structure.

Mainly, these are used to create the complex data structures such as linked lists, trees, graphs and so on.

The members of the structure can be accessed by using a special operator called arrow operator ( -> ).

Declaration

Following is the declaration for pointer to structure −

struct tagname *ptr;

For example, struct student *s;

Accessing

You can access pointer to structure by using the following −

Ptr-> membername;

For example, s->sno, s->sname, s->marks;

Example

Following is the C program of the pointer structures −

#include<stdio.h>
struct student{
   int sno;
   char sname[30];
   float marks;
};
main ( ){
   struct student s;
   struct student *st;
   printf("enter sno, sname, marks:");
   scanf ("%d%s%f", & s.sno, s.sname, &s. marks);
   st = &s;
   printf ("details of the student are");
   printf ("Number = %d\n", st ->sno);
   printf ("name = %s\n", st->sname);
   printf ("marks =%f\n", st ->marks);
   getch ( );
}

Output

When the above program is executed, it produces the following result −

enter sno, sname, marks:1 priya 34
details of the student areNumber = 1
name = priya
marks =34.000000
raja
Updated on 09-Mar-2021 09:45:25

Advertisements