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.



Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
Complex pointer
Generic pointer
Null pointer

C tutorial

3 comments:

ranjith said...

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


how **array beacme *array[1] after ++(**ptr)...
plz explain.....

menu said...

#include

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:garbage
Explanation: Address of auto variable can be member of an array.

but here i,j,k are not initialized

Arun Gupta said...

your both answer wrong plzz correct it

***ptr =**array=*array[0]=*(&i)=i;
bt previously we increment the (&i) with statement ++(**ptr)-thats means points next static int memory location so ans is 0 in first question.