clanguage07@gmail.com

clanguage07@gmail.com

Sunday, September 8, 2013

Pointer to array of integer in c



Pointer to array of integers: A pointer to such an array which contents are integer numbers is known as pointer to array of integer.


What will be output if you will execute following code?


#include<stdio.h>

int main(){

static int i,j,k;
int *(*ptr)[];
int *array[3]={&i,&j,&k};

ptr=&array;
j=i+++k+10;
++(**ptr);

printf("%d",***ptr);


return 0;
}

Output: 10
Explanation:

In this example:

array []: It is array of size three and its content are address of integer.

ptr: It is pointer to array which content are address of integer.

Pictorial representation above declaration:







Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

j=i+++k+10
=i++ + k+10
=0 +0 +10=10

***ptr = *** (&array) //ptr=&array
= **array //From rule *&p=p
//From rule array [0] =*(array+0) and ++ (**ptr)
=*array [1]
=*&j
=j
=10



What will be output if you will execute following code?


#include<stdio.h>

int main(){

int i,j,k;
int *(*ptr)[];
int *array[3]={&i,&j,&k};

ptr=&array;
j=i+++k+10;
++(**ptr);

printf("%d",***ptr);


return 0;
}

Output: Compiler error
Explanation: Address of auto variable cannot be member of an array.

No comments:

Post a Comment

GET MORE INFORMATION

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