Pointer to array of character in c
What will be output if you will execute following code?
#include<stdio.h>
#include<stdio.h>
char display(char (*)[]);
int main(){
char c;
char character[]={65,66,67,68};
char (*ptr)[]=&character;
c=display(ptr);
printf("%c",c);
return 0;
return 0;
}
char display(char (*s)[]){
**s+=2;
return **s;
}
Output: C
Explanation: Here function display is passing pointer to array of characters and returning char data type.
**s+=2
=>**s=**s+2
=>**ptr=**ptr+2 //s=ptr
=>**&character= **&character+2 //ptr=&character
=>*character=*character+2 //from rule *&p =p
=>character[0]=character[0]+2 //from rule *(p+i)=p[i]
=>character [0] =67
**s=character [0] =67
Note: ASCII value of ‘C’ is 67
Pointer to array of integer in c
What will be output if you will execute following code?
#include<stdio.h>
#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;
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>
#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;
return 0;
}
Output: Compiler error
Explanation: Address of auto variable cannot be member of an array.Pointer to array of array in c
Examples of pointer to array of array in c:
What will be output if you will execute following code?
#include<stdio.h>
What will be output if you will execute following code?
#include<stdio.h>
int main(){
static float farray[][3]={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f};
float (*array[3])[3]={&farray[0],&farray[1],&farray[2]};
float (*(*ptr)[])[3]=&array;
printf("%f ",2[(*(**ptr+1))]);
return 0;
return 0;
}
Output: 5.000000
Explanation:
In this example:
farray [][3]: It is two dimension array and its content are float constants.
array [3]:It is one dimension array and its content are address of such one dimension array which content are float constant.
ptr:
It is pointer to one dimension array which content are address of such
one dimension array which content are float constant.
Pictorial representation:
Note:
In the above figure upper part of box represent content and lower part
represent memory address. We have assumed arbitrary address.
2[(*(**ptr+1))]
= (*(**ptr+1)) [2]
= (*(**&array+1)) [2]
= (*(*array+1)) [2]
= (*(array [0] +1)) [2]
= (*(&farray [0] +1)) [2]
=&farray [0] [1] [2]
=*&farray [1] [2]
=farray [1] [2]
It is 1*(3) +2=5th element of farray starting from zero which is 5.0f
0 comments:
Post a Comment
Dont Forget for Commets