Pointer to 2D Array पॉइंटर टू 2D ऐरे

 A pointer variable which stores the base address (memory address of the first element) of a 2D array is commonly used to access the elements of that array. It is explained as a pointer to 2D array in introductory C programming.

एक पॉइंटर वेरिएबल जो 2D ऐरे के बेस एड्रेस अर्थात उसके पहले एलिमेंट का मेमोरी एड्रेस स्टोर करता है, उसे सामान्यतः पॉइंटर टू 2D ऐरे के रूप में समझाया जाता है।

Syntax:

data_type array_name[row_size][column_size], *pointer_name;

pointer_name = &array_name[0][0];

Example:

int mat[3][3], *p;

p = &mat[0][0];

In the above example, pointer p stores the memory address of the first element of 2D array mat, that is, mat[0][0].

उपरोक्त उदाहरण में पॉइंटर p, 2D ऐरे mat के पहले एलिमेंट अर्थात mat[0][0] का मेमोरी एड्रेस स्टोर करता है।

C Program for Pointer to 2D Array

#include <stdio.h>
#include <conio.h>

void main()
{
    int mat[3][3] = {
        {2, 4, 5},
        {1, 0, 1},
        {3, 7, 3}
    };

    int *p, i, j;

    clrscr();

    p = &mat[0][0];

    printf("Accessing 2D array mat through pointer p\n");

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("%d ", *p++);
        }

        printf("\n");
    }

    getch();
}

Output:

Accessing 2D array mat through pointer p
2 4 5
1 0 1
3 7 3

Explanation:

In the above program:

  • mat[3][3] declares a 2D integer array having 3 rows and 3 columns.

  • int *p; declares an integer pointer.

  • p = &mat[0][0]; stores the address of the first element of array mat in pointer p.

  • *p accesses the value of the element pointed to by p.

  • *p++ accesses the current element and then increments the pointer to the next integer element.

  • The nested loops display all the elements of the 2D array through pointer p.

उपरोक्त प्रोग्राम में:

  • mat[3][3] एक 2D इन्टिजर ऐरे डिक्लेयर करता है जिसमें 3 रो एवं 3 कॉलम हैं।

  • int *p; एक इन्टिजर पॉइंटर डिक्लेयर करता है।

  • p = &mat[0][0]; ऐरे mat के पहले एलिमेंट का एड्रेस पॉइंटर p में स्टोर करता है।

  • *p पॉइंटर द्वारा पॉइंट किए जाने वाले एलिमेंट की वैल्यू एक्सेस करता है।

  • *p++ पहले वर्तमान एलिमेंट की वैल्यू एक्सेस करता है और फिर पॉइंटर को अगले इन्टिजर एलिमेंट पर ले जाता है।

  • नेस्टेड लूप पॉइंटर p की सहायता से 2D ऐरे के सभी एलिमेंट डिस्प्ले करते हैं।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Top High-Paying Tech Skills to Learn in 2025

Complete C Programming Syllabus | Advanced C Roadmap for Students