Pointers and Structures in C Programming in Hindi

इस tutorial में हम सीखेंगे की कैसे आप C programming में pointers और structures को एक साथ use कर सकते हो और कैसे आप pointers की help से structure members को access कर सकते हो.

इस tutorial को समझने के लिए आपको पहले C structures और C pointers की जानकारी होना जरूरी है इसलिए अगर आपने हमारे इन दोनों topics के tutorial नहीं पढ़ें तो नीचे links पर click करके पहले उन्हें जरूर पढ़ लें.

Read: Structures in C Programming in Hindi

Read: Pointers in C Programming in Hindi

Structure Pointers Variable in C

जैसे हम int data type के variable के address को store करने के लिए int data type का ही pointer लेते हैं ठीक ऐसे ही आपको किसी structure variable का address store करने के लिए आपको उसी structure का pointer variable declare करना होगा.

जैसे हमने नीचे stu struct students का normal variable है और ptr struct students का pointer variable है.

Structure variables declare करने के बाद हमने struct student type के pointer ptr को struct student type के variable stu का address assign कराया है.

struct students
{
    int roll;
    char name[20];
    float cgpa;
};

int main()
{
    struct students stu, *ptr;
    
    ptr = &stu;
}

आप pointer ptr में struct students type के किसी भी variable के address को store करा सकते हैं और फिर उस address पर मौजूद struct students के किसी members को pointer ptr से access कर सकते हो.

Access Structure Members using Pointers in C

आप 2 तरह से structure pointer से structure members को access कर सकते हो.

  1. Arrow operator ( -> ) का उपयोग करके
  2. Indirection operator ( * ) के साथ dot operator ( . ) का उपयोग करके 

Using Arrow Operator

ज्यादातर programmers pointer से structure members को access करने के लिए arrow operator का उपयोग करते हैं क्योंकि जो दूसरा तरीका थोड़ा confusing और कम readable होता है.

Syntax:

pointer->member;

Input and Output Structure Members using Arrow Operator:

#include <stdio.h>
struct students
{
    int roll;
    char name[20];
    float cgpa;
};

int main()
{
    struct students stu, *ptr;

    ptr = &stu;

    printf("Enter Student Roll No. : ");
    scanf("%d",&ptr->roll);

    printf("Enter Student Name : ");
    scanf("%s",ptr->name);

    printf("Enter Student CGPA : ");
    scanf("%f",&ptr->cgpa);

    printf("\nStudent Records:\n");
    printf("Roll : %d\n",ptr->roll);
    printf("Name : %s\n",ptr->name);
    printf("CGPA : %f\n",ptr->cgpa);

    return 0;
}

Output:

Enter Student Roll No. : 12
Enter Student Name : Karan
Enter Student CGPA : 9.8

Student Records:
Roll : 12
Name : Karan
CGPA : 9.800000

Using Indirection (*) Operator and Dot (.) Operator

अगर आप pointer के साथ indirection ( * ) और dot ( . ) operator का use करके structure members को access करना चाहते हो तो सिर्फ इस बात का विशेष ध्यान रखें की * और pointer को एक साथ parenthese के अंदर रखे जैसे (*ptr).

Syntax:

(*pointer).member;

Example Program:

#include <stdio.h>
struct students
{
    int roll;
    char name[20];
    float cgpa;
};

int main()
{
    struct students stu, *ptr;

    ptr = &stu;

    printf("Enter Student Roll No. : ");
    scanf("%d",&(*ptr).roll);

    printf("Enter Student Name : ");
    scanf("%s",(*ptr).name);

    printf("Enter Student CGPA : ");
    scanf("%f",&(*ptr).cgpa);

    printf("Student Records:\n");
    printf("Roll : %d\n",(*ptr).roll);
    printf("Name : %s\n",(*ptr).name);
    printf("CGPA : %f\n",(*ptr).cgpa);

    return 0;
}

Output:

Enter Student Roll No. : 12
Enter Student Name : Karan
Enter Student CGPA : 9.8

Student Records:
Roll : 12
Name : Karan
CGPA : 9.800000