Categories: C

Pointer to structure

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.

Example:
#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 = &sub; /* 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;  
      
}  
Output:
 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

Follow Me

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

Share
Published by
Rabecca Fatima

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago