Advanced Pointer

12
Advanced Pointer Question 1 WRONG void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; } 10 20 C Compiler error D Runtime Error Discuss it Question 1 Explanation: Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains uneffected. If we want to change a local pointer of one function inside another function, then we must pass pointer to the pointer. By passing the pointer to the pointer, we can change pointer to point to something else. See the following program as an example. void fun(int **pptr) { static int q = 10;

description

Advanced Pointer

Transcript of Advanced Pointer

Advanced PointerQuestion 1WRONG

void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; }

10

20

CCompiler error

DRuntime Error

Discuss it

Question 1 Explanation:Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains uneffected. If we want to change a local pointer of one function inside another function, then we must pass pointer to the pointer. By passing the pointer to the pointer, we can change pointer to point to something else. See the following program as an example.void fun(int **pptr){ static int q = 10; *pptr = &q;}

int main(){ int r = 20; int *p = &r; fun(&p); printf("%d", *p); return 0;}

In the above example, the function fun() expects a double pointer (pointer to a pointer to an integer). Fun() modifies the value at address pptr. The value at address pptr is pointer p as we pass adderess of p to fun(). In fun(), value at pptr is changed to address of q. Therefore, pointer p of main() is changed to point to a new variable q. Also, note that the program wont cause any out of scope problem because q is astaticvariable. Static variables exist in memory even after functions return. For anautovariable, we might have seen some unexpected output because auto variable may not exist in memory after functions return.Question 2WRONG

Assume sizeof an integer and a pointer is 4 byte. Output?#include #define R 10#define C 20int main(){int (*p)[R][C];printf("%d", sizeof(*p));getchar();return 0;}

200

B4

800

D80

Discuss it

Question 2 Explanation:Output is 10*20*sizeof(int) which is 800 for compilers with integer size as 4 bytes. When a pointer is de-referenced using *, it yields type of the object being pointed. In the present case, it is an array of array of integers. So, it prints R*C*sizeof(int).Question 3CORRECT

#include int main(){int a[5] = {1,2,3,4,5};int *ptr = (int*)(&a+1);printf("%d %d", *(a+1), *(ptr-1));return 0;}

2 5

BGarbage Value

CCompiler Error

DSegmentation Fault

Discuss it

Question 3 Explanation:The program prints 2 5. Since compilers convert array operations in pointers before accessing the array elements, (a+1) points to 2. The expression (&a + 1) is actually an address just after end of array ( after address of 5 ) because &a contains address of an item of size 5*integer_size and when we do (&a + 1) the pointer is incremented by 5*integer_size. ptr is type-casted to int * so when we do ptr -1, we get address of 5Question 4CORRECT

#include char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"};char **cp[] = {c+3, c+2, c+1, c};char ***cpp = cp;int main(){printf("%s ", **++cpp);printf("%s ", *--*++cpp+3);printf("%s ", *cpp[-2]+3);printf("%s ", cpp[-1][-1]+1);return 0;}

TEST sQuiz Z CQ

BMCQ Quiz Z CQ

CTEST Quiz Z CQ

DGarbageValuesQuiz Z CQ

Discuss it

Question 4 Explanation:Let us first consider **++cpp.Precedence of prefix increment and de-reference is sameand associativity of both of them is right to left. So the expression is evaluated as **(++cpp). So cpp points to c+2. So we get "TEST" as output. Note the de-reference operator twice. Similarly, you may try other expressions yourself with the help ofprecedence table.Question 5CORRECT

Predict the output#include #include #include void fun(char** str_ref){str_ref++;}int main(){char *str = (void *)malloc(100*sizeof(char));strcpy(str, "GeeksQuiz");fun(&str);puts(str);free(str);return 0;}

GeeksQuiz

BeeksQuiz

CGarbage Value

DCompiler Error

Discuss it

Question 5 Explanation:Note that str_ref is a local variable to fun(). When we do str_ref++, it only changes the local variable str_ref. We can change str pointer using dereference operator *. For example, the following program prints "eeksQuiz"#include #include #include

void fun(char** str_ref){ (*str_ref)++;}

int main(){ char *str = (void *)malloc(100*sizeof(char)); strcpy(str, "GeeksQuiz"); fun(&str); puts(str); free(str); return 0;}Question 6CORRECT

#include int main(){int a[][3] = {1, 2, 3, 4, 5, 6};int (*ptr)[3] = a;printf("%d %d ", (*ptr)[1], (*ptr)[2]);++ptr;printf("%d %d\n", (*ptr)[1], (*ptr)[2]);return 0;}

2 3 5 6

B2 3 4 5

C4 5 0 0

Dnone of the above

Discuss it

Question 7WRONG

Assume that the size of int is 4.#include void f(char**);int main(){char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };f(argv);return 0;}void f(char **p){char *t;t = (p += sizeof(int))[-1];printf("%s\n", t);}

ab

Bcd

Cef

gh

Discuss it

Question 7 Explanation:The expression (p += sizeof(int))[-1] can be written as (p += 4)[-1] which can be written as (p = p+4)[-] which returns address p+3 which is address of fourth element in argv[].Question 8CORRECT

#include int main(){int a[][3] = {1, 2, 3, 4, 5, 6};int (*ptr)[3] = a;printf("%d %d ", (*ptr)[1], (*ptr)[2]);++ptr;printf("%d %d\n", (*ptr)[1], (*ptr)[2]);return 0;}

2 3 5 6

B2 3 4 5

C4 5 0 0

Dnone of the above

Discuss it

Question 9WRONG

#include int main(void){int i;int *ptr = (int *) malloc(5 * sizeof(int));for (i=0; i