Programming in C: Unit III (b): Pointers

Pointers and 3D Arrays

with Example C Program

In this section, we will see how pointers can be used to access a three-dimensional array.

POINTERS AND 3D ARRAYS

In this section, we will see how pointers can be used to access a three-dimensional array. We have seen that pointer to a one-dimensional array can be declared as

int arr[]={1,2,3,4,5);

int *parr;

parr = arr;

Similarly, pointer to a two-dimensional array can be declared as

int arr [2] [2]={{1,2}, {3, 4}};

int (*parr) [2];

parr = arr;

A pointer to a three-dimensional array can be declared as

int arr [2] [2] [2]={1,2,3,4,5,6,7,8};

int (*parr) [2] [2];

parr = arr;

We can access an element of a three-dimensional array by writing,

arr[i] [j] [k] = *(*(* (arr+i)+j)+k)

Look at the code given below which illustrates the use of a pointer to a three-dimensional array.

#include <stdio.h>

#include <conio.h>

main()

{

int i,j,k;

int arr [2] [2] [2];

int (*parr) [2] [2] = arr;

clrscr();

printf("\n Enter the elements of a 2 × 2 × 2 array:");

for (i = 0; i < 2; i++)

{

for (j = 0; j < 2; j++)

{

for (k = 0; k < 2; k++)

scanf("%d", &arr[i] [j] [k]);

}

}

awode printf("\n The elements of the 2 × 2 × 2 narray are: ");

for (i 0; i < 2; i++)

{

 for (j = 0; j < 2; j++)

{

for (k = 0; k < 2; k++)

printf("%d", *(*(* (parr+i)+j)+k));

}

}

return 0;

getch();

}

Output

Enter the elements of a 2 × 2 × 2 array: 1 2 3 4 5 6 7 8

The elements of the 2 × 2 × 2 array are: 1 2 3 4 5 6 7 8

Note

In the printf statement, you could also have used * (* (* (arr+i)+j)+k)) instead of *(*(* (parr+i)+j)+k)).

Programming in C: Unit III (b): Pointers : Tag: : with Example C Program - Pointers and 3D Arrays


Programming in C: Unit III (b): Pointers



Under Subject


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation



Related Subjects


Professional English II

HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Statistics and Numerical Methods

MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation


Engineering Graphics

GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Physics for Electrical Engineering

PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Basic Civil and Mechanical Engineering

BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Electric Circuit Analysis

EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Physics for Electronics Engineering

PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Electrical and Instrumentation Engineering

BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Circuit Analysis

EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Materials Science

PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Physics for Civil Engineering

PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Basic Electrical, Electronics and Instrumentation Engineering

BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Physics for Information Science

PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation