Thursday

pointer to two,three dimensional array


 

Pointer to two dimensional array in c programming

Examples of pointers to 2 dimensional array:


What will be output if you will execute following code?


#include<stdio.h>
void main(){

long array[][3]={7l,14l,21l,28l,35l,42l};
long int (*ptr)[2][3]=&array;

printf("%li ",-0[1[0[ptr]]]);


return 0;
}

Output: -28
Explanation:

-0[1[0[ptr]]]
=-1[0[ptr]][0] //From rule array[i]=i[array]
=-0[ptr][1][0]
=-ptr [0] [1] [0]
=-*ptr [0] [1] //From rule array[i]=*(array+i)
=-*(&array) [0] [1]
=-(&array) [0] [1][0]
=-(*&array)[1][0] //From rule *&p=p
=-array[1][0]

array[1][0] means 1*(3)+ 0 = 3rd element of array starting from zero i.e. 28

 

Pointer to three dimensional array in c programming



Examples of pointers to 3 dimensional array:


#include<stdio.h>
int main(){

const array[2][3][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int const (*ptr)[2][3][3]=&array;

printf("%d ",*(*(*ptr)[1]+2));


return 0;
}

Output: 11
Explanation:

In this example:
array [2][3][3]:It is three dimensional array and its content are constant integers.
ptr: It is pointer to such three dimensional array whose content are constant integer.

Pictorial representation:

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |