clanguage07@gmail.com

clanguage07@gmail.com

Sunday, September 8, 2013

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

No comments:

Post a Comment

GET MORE INFORMATION

http://ads.qadservice.com/t?id=c2168e05-8974-4816-872a-91936ff7379d&size=1024x768&drct=true