Pointers Test your understanding 1 p 1 void

  • Slides: 41
Download presentation
Pointers Test your understanding 1

Pointers Test your understanding 1

p 1 • void main(){ • printf("%s", "c" "question" "bank"); • } 2

p 1 • void main(){ • printf("%s", "c" "question" "bank"); • } 2

o 1 • cquestionbank 3

o 1 • cquestionbank 3

p 2 1. 2. 3. 4. void main(){ char *str="c-pointer"; printf("%*. *s", 10, 7,

p 2 1. 2. 3. 4. void main(){ char *str="c-pointer"; printf("%*. *s", 10, 7, str); } 4

02 • c-point 5

02 • c-point 5

P 3 : What is meaning of following declaration? 1) int(*ptr[5])(); 2) int(*(*ptr 1)())[2];

P 3 : What is meaning of following declaration? 1) int(*ptr[5])(); 2) int(*(*ptr 1)())[2]; 6

s 3 1. ptr is array of pointer to function. 2. ptr is pointer

s 3 1. ptr is array of pointer to function. 2. ptr is pointer to such function which return type is pointer to an array. 7

p 4 1. 2. 3. 4. 5. 6. 7. #include<stdio. h> int main(){ int

p 4 1. 2. 3. 4. 5. 6. 7. #include<stdio. h> int main(){ int a=5, b=10, c=15; int *arr[]={&a, &b, &c}; printf("%d", *arr[1]); return 0; } 8

04 • Compiler error • Explanation: Array element cannot be address of auto variable.

04 • Compiler error • Explanation: Array element cannot be address of auto variable. It can be address of static or extern variables. • 9

p 5 1. 2. 3. 4. #include<stdio. h> int main(){ int a[2][4]={3, 6, 9,

p 5 1. 2. 3. 4. #include<stdio. h> int main(){ int a[2][4]={3, 6, 9, 12, 15, 18, 21, 24}; printf("%d %d %d", *(a[1]+2), *(*(a+1)+2), 2[1[a]]); 5. return 0; 6. } 10

o 5 • 21 21 21 Explanation: In c, a [1][2] =*(a [1] +2)

o 5 • 21 21 21 Explanation: In c, a [1][2] =*(a [1] +2) =*(*(a+1) +2) =2[a [1]] =2[1[a]] Now, a [1] [2] means 1*(4) +2=6 th element of an array staring from zero i. e. 21. 11

p 6 1. 2. 3. 4. 5. 6. 7. 8. #include<stdio. h> int main(){

p 6 1. 2. 3. 4. 5. 6. 7. 8. #include<stdio. h> int main(){ const int x=25; int * const p=&x; *p=2*x; printf("%d", x); return 0; } 12

06 • 50 ->const keyword in c doesn’t make any variable as constant but

06 • 50 ->const keyword in c doesn’t make any variable as constant but it only makes the variable as read only. With the help of pointer we can modify the const variable. In this example pointer p is pointing to address of variable x. In the following line: int * const p=&x; p is constant pointer while content of p i. e. *p is not constant. *p=2*x put the value 50 at the memory location of variable x. 13

p 7 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

p 7 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. #include<stdio. h> int main(){ static char *s[3]={"math", "phy", "che"}; typedef char *( *ppp)[3]; static ppp p 1=&s, p 2=&s, p 3=&s; char * (*(*array[3]))[3]={&p 1, &p 2, &p 3}; char * (*(*(*ptr)[3]))[3]=&array; p 2+=1; p 3+=2; printf("%s", (***ptr[0])[2]); return 0; } 14

o 7 • che Explanation: Here ptr is pointer to array of pointer to

o 7 • che Explanation: Here ptr is pointer to array of pointer to string. P 1, p 2, p 3 are pointers to array of string. array[3] is array which contain pointer to array of string. As we know p[i]=*(p+i) (***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2] =(***(&array))[2] //ptr=&array =(**array)[2] //From rule *&p=p =(**(&p 1))[2] //array=&p 1 =(*p 1)[2] =(*&s)[2] //p 1=&s =s[2]=”che” 15

P 8 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

P 8 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. #include<stdio. h> int display(); int(*array[3])(); int(*(*ptr)[3])(); int main(){ array[0]=display; array[1]=getch; ptr=&array; printf("%d", (**ptr)()); (*(*ptr+1))(); return 0; } int display(){ int x=5; return x++; } 16

o 8 • • • • 5 Explanation: In this example: array []: It

o 8 • • • • 5 Explanation: In this example: array []: It is array of pointer to such function which parameter is void and return type is int data type. ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data. (**ptr)() = (** (&array)) () //ptr=&array = (*array) () // from rule *&p=p =array [0] () //from rule *(p+i)=p[i] =display () //array[0]=display (*(*ptr+1))() =(*(*&array+1))() //ptr=&array =*(array+1) () // from rule *&p=p =array [1] () //from rule *(p+i)=p[i] =getch () //array[1]=getch 17

p 9 • • • #include<stdio. h> int main(){ char arr[]="C Question Bank"; float

p 9 • • • #include<stdio. h> int main(){ char arr[]="C Question Bank"; float *fptr; fptr=(float *)arr; fptr++; printf("%s", fptr); return 0; } 18

09 • estion Bank. 19

09 • estion Bank. 19

p 10 1. void foo(int*); 2. int main() 3. { 4. int i =

p 10 1. void foo(int*); 2. int main() 3. { 4. int i = 10; 5. foo((&i)++); 6. } 7. void foo(int *p) 8. { 9. printf("%dn", *p); 10. } 20

010 • Compile time error 21

010 • Compile time error 21

p 11 • • • void foo(int*); int main() { int i = 10,

p 11 • • • void foo(int*); int main() { int i = 10, *p = &i; foo(p++); } void foo(int *p) { printf("%dn", *p); } 22

011 • 10 23

011 • 10 23

p 12 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

p 12 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. #include <stdio. h> int main() { int i = 97, *p = &i; foo(&i); printf("%d ", *p); } void foo(int *p) { int j = 2; p = &j; printf("%d ", *p); } 24

012 • 2 97 25

012 • 2 97 25

p 13 • Which of the following are correct syntaxes to send an array

p 13 • Which of the following are correct syntaxes to send an array as a parameter to function: a) func(&array); b) func(array); c) func(*array); d) func(array[size]); 26

013 • Answer: a & b 27

013 • Answer: a & b 27

p 14 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

p 14 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. #include <stdio. h> int main() { int i = 10; int *const p = &i; foo(&p); printf("%dn", *p); } void foo(int **p) { int j = 11; *p = &j; printf("%dn", **p); } 28

014 • 11 29

014 • 11 29

p 15 • • #include <stdio. h> int main() { char *str="Include. Help"; printf("%cn",

p 15 • • #include <stdio. h> int main() { char *str="Include. Help"; printf("%cn", *&*str); return 0; } 30

015 • I • & is a reference operator, * is de-reference operator, We

015 • I • & is a reference operator, * is de-reference operator, We can use these operators any number of times. str points the first character of Include. Help, *str points "I", * & again reference and de-reference the value of str. 31

p 16 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

p 16 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. #include <stdio. h> int main() { int i. Val; char c. Val; void *ptr; // void pointer i. Val=50; c. Val=65; ptr=&i. Val; printf("value =%d, size= %dn", *(int*)ptr, sizeof(ptr)); ptr=&c. Val; printf("value =%d, size= %dn", *(char*)ptr, sizeof(ptr)); return 0; } 32

016 • value =50, size= 4 value =65, size= 4 void pointer can be

016 • value =50, size= 4 value =65, size= 4 void pointer can be type casted to any type of data type, and pointer takes 4 bytes (On 32 bit compiler). to print value using void pointer, you will have to write like this *(data_type*)void_ptr; . 33

P 17 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

P 17 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. #include <stdio. h> int main() { char *str []={"AAAAA", "BBBBB", "CCCCC", "DDDDD"}; char **sptr []={str+3, str+2, str+1, str}; char ***pp; pp=sptr; ++pp; printf("%s", **++pp+2); return 0; } 34

017 • BBB *str is a array pointer of string, **sptr is array pointer(double

017 • BBB *str is a array pointer of string, **sptr is array pointer(double pointer) that is pointing to strings in reverse order. ***pp also a pointer that is pointing sptr base address. ++pp will point to 1 st index of sptr that contain str+2 ("CCCCC"). in printf("%s", **++pp+2); ++pp will point to str+1, and **++pp, value stored @ str+1 ("BBBBB). and (**++pp)+2 will point the 2 nd index of "BBBBB", hence BBB will print. 35

p 18 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

p 18 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. #include <stdio. h> char* str. Fun(void) { char *str="Include. Help"; return str; } int main() { char *x; x=str. Fun(); printf("str value = %s", x); return 0; } 36

018 • str value= Include. Help 37

018 • str value= Include. Help 37

p 19 1. 2. 3. 4. 5. 6. 7. 8. #include <stdio. h> int

p 19 1. 2. 3. 4. 5. 6. 7. 8. #include <stdio. h> int main() { int a=10, b=2; int *pa=&a, *pb=&b; printf("value = %d", *pa/*pb); return 0; } 38

019 • ERROR: unexpected end of file found in comment. The compiler is treated

019 • ERROR: unexpected end of file found in comment. The compiler is treated the operator / and * as /*, that happens to be the starting of comment. To fix the error, use either *pa/ *pb (space between operators) or *pa/(*pb). 39

p 20 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

p 20 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. #include <stdio. h> void fun(int *ptr) { *ptr=100; } int main() { int num=50; int *pp=# fun(& *pp); printf("%d, %d", num, *pp); return 0; } 40

020 • 100, 100 41

020 • 100, 100 41