The structure pointer points to the memory block address where the Structure is stored.
It’s used to make complex data structures like linked lists, trees, and graphs.
The structure’s members can be accessed using an arrow operator (->).
we can declare a struct pointer in the following manner:
struct structureName *p;
here, *p represents the pointer.
Accessing is don by :
p-> membername;
OR
(*p).membername;
For, instance : p->name, p->rollno, (*p).sub_name, etc.
#include <stdio.h>
// create a structure Subject using the struct keyword
struct Subject
{
// declare the member of the Course structure
char s_name[30];
int s_id;
char s_duration[50];
char s_type[50];
};
int main()
{
struct Subject sub; // declare the Subject variable
struct Subject *ptr; // create a pointer variable (*ptr)
ptr = ⊂ /* ptr variable pointing to the address of the structure variable sub */
strcpy (sub.s_name, " Computer Science");
sub.s_id = 301;
strcpy (sub.s_duration, "3 Months");
strcpy (sub.s_type, " Subjective");
// print the details of the Subject;
printf (" Subject Name: %s\t ", (*ptr).s_name);
printf (" \n Subject Id: %d\t ", (*ptr).s_id);
printf (" \n Duration of the Subject: %s\t ", (*ptr).s_duration);
printf (" \n Type of the Subject: %s\t ", (*ptr).s_type);
return 0;
}
Subject Name: Computer Science
Subject Id: 301
Duration of the Subject: 3 Months
Type of the Subject: Subjective
In the preceding program, we created the Subject structure, which contains various data elements such as s name (char), s id (int), s duration (char), and s type (char). Sub is the structure variable in this case, and ptr is the structure pointer variable that points to the address of the sub variable, as in ptr = &sub. In this manner, each *ptr obtains the address of a member of the Subject structure.
Note: also read about the Pointer to array
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…