In this article we will learn to implement a C program to demonstrate accessing a structure using a pointer. A C program is provided below that illustrates how to access a structure using a pointer.
Program is as follows:
/*
* C program to demonstrate accessing a structure using a pointer
* Author: P.S.SuryaTeja
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int id;
char name[20];
};
int main(int argc, char **argv)
{
struct student *stp;
struct student st;
stp = &st;
printf("Enter student id: ");
scanf("%d", &stp->id); //&stp->id is same as writing &(*stp).id
fflush(stdin);
printf("Enter student name: ");
gets(stp->name);
printf("\n***Student Details***");
printf("\nStudent id: %d", stp->id);
printf("\nStudent name: ");
puts(stp->name);
getch();
return 0;
}
Input and output for the above program is as follows:
Enter student id: 101
Enter student name: teja
***Student Details***
Student id: 101
Student name: teja
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply