Question 1: #include<stdio.h> int main() { int a[] = {1, 2, 3, 4, 5, 6}; int *ptr = ( int *)(&a+1); printf ( "%d " , *(ptr-1) ); getchar (); return 0; } |
Output: 6
&a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] + sizeof(a)”. And this value is typecasted to int *. So ptr – 1 points to last element of a[]
Question 2:
Output: 10*20*sizeof(int) which is “800″ for compilers with integer size as 4 bytes.
The pointer p is de-referenced, hence it yields type of the object. In the present case, it is an array of array of integers. So, it prints R*C*sizeof(int).
&a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] + sizeof(a)”. And this value is typecasted to int *. So ptr – 1 points to last element of a[]
Question 2:
#include<stdio.h> #define R 10 #define C 20 int main() { int (*p)[R][C]; printf ( "%d" , sizeof (*p)); getchar (); return 0; } |
The pointer p is de-referenced, hence it yields type of the object. In the present case, it is an array of array of integers. So, it prints R*C*sizeof(int).
No comments:
Post a Comment