Updated:13/02/2024 by Computer Hope
Go Back
Want to learn how we can implement strucure in c and There are two ways that members of a structure can be retrieved from pointers.
Utilising a pointer with the dot and asterisk operators. using a pointer with the arrow operator (->).
A structure pointer's declaration is comparable to that of a structure variable. Thus, we are able to declare the variable and structure pointer both within and outside of the main() method. In C, we use the asterisk (*) symbol before the variable name to declare a pointer variable.
//Write a program to demonstarte of STRUCTURE WITH POINTER
#include<stdio.h>
struct student
{
int sno,m;
char sname[20];
float per;
};
void main()
{
struct student s1 , *p;
p = &s1;
printf("\n enter sno,m,sname of student");
scanf("%d%d%s" , &p->sno , &p->m , p->sname );
p->per=p->m/3.0;
printf("\n %d %d %s" , p->sno , p->m , p->sname , p->per );
}
In the above program, we have created the Student structure that contains different data elements like sname (char), sno (int), per (float), and m (int). In this, the sname is the structure variable, and ptr is the structure pointer variable that points to the address of the sub variable like ptr = &s1. In this way, each *ptr is accessing the roll number of the Student structure's member.