The structure pointer saves the addition of the entire structure.
It is used to create complex data structures, such as linked lists, trees, graphs, etc.
Members of a structure can be accessed using a special operator called the arrow operator ( -> ).
The following is the declaration of a pointer to a structure in C programming -
struct tagname *ptr;
For example- struct Student *s -
The following explains how to access the structure pointer.
Ptr-> membername;
For example - s->sno, s->sname, s->marks;
The following program shows the usage of structure pointers- p>
#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</p><p>", st ->sno); printf ("name = %s</p><p>", st->sname); printf ("marks =%f</p><p>", st ->marks); getch ( ); }
Let us run the above program, it will produce the following result-
enter sno, sname, marks:1 Lucky 98 details of the student are: Number = 1 name = Lucky marks =98.000000
Consider another example which explains structure pointers function.
Real-time demonstration
#include<stdio.h> struct person{ int age; float weight; }; int main(){ struct person *personPtr, person1; personPtr = &person1; printf("Enter age: "); scanf("%d", &personPtr->age); printf("Enter weight: "); scanf("%f", &personPtr->weight); printf("Displaying:</p><p>"); printf("Age: %d</p><p>", personPtr->age); printf("weight: %f", personPtr->weight); return 0; }
Let us run the above program, it will produce the following results -
Enter age: 45 Enter weight: 60 Displaying: Age: 45 weight: 60.000000
The above is the detailed content of In C language, a pointer is a pointer to a structure. For more information, please follow other related articles on the PHP Chinese website!