coderz.py

Keep Coding Keep Cheering!

Pointer to structure

Matrix multiplication

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

Leave a Comment

Advertisement