C Advanced Pointer
Question 1 |
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 | |
Compiler error | |
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;
*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 won’t cause any out of scope problem because q is a static variable. Static variables exist in memory even after functions return. For an auto variable, we might have seen some unexpected output because auto variable may not exist in memory after functions return.Question 2 |
Assume sizeof an integer and a pointer is 4 byte. Output?
#include <stdio.h>
#define R 10
#define C 20
int main()
{
int (*p)[R][C];
printf("%d", sizeof(*p));
getchar();
return 0;
}
200 | |
4 | |
800 | |
80 |
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 3 |
#include <stdio.h>
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 | |
Garbage Value | |
Compiler Error | |
Segmentation 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 5
Question 4 |
#include <stdio.h>
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 | |
MCQ Quiz Z CQ | |
TEST Quiz Z CQ | |
GarbageValue sQuiz Z CQ |
Discuss it
Question 4 Explanation:
Let us first consider **++cpp. Precedence of prefix increment and de-reference is same and 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 of precedence table.
Question 5 |
Predict the output
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
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 | |
eeksQuiz | |
Garbage Value | |
Compiler 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 <string.h>
#include <stdio.h>
#include <stdlib.h>
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 6 |
Assume that the size of int is 4.
#include <stdio.h>
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("%sn", t);
}
ab | |
cd | |
ef | |
gh |
Discuss it
Question 6 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 7 |
#include <stdio.h>
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 %dn", (*ptr)[1], (*ptr)[2]);
return 0;
}
2 3 5 6 | |
2 3 4 5 | |
4 5 0 0 | |
none of the above |
Discuss it
Question 8 |
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int *ptr = (int *) malloc(5 * sizeof(int));
for (i=0; i<5; i++)
*(ptr + i) = i;
printf("%d ", *ptr++);
printf("%d ", (*ptr)++);
printf("%d ", *ptr);
printf("%d ", *++ptr);
printf("%d ", ++*ptr);
}
Compiler Error | |
0 1 2 2 3 | |
0 1 2 3 4 | |
1 2 3 4 5 |
Discuss it
Question 8 Explanation:
The important things to remember for handling such questions are
1) Prefix ++ and * operators have same precedence and right to left associativity.
2) Postfix ++ has higher precedence than the above two mentioned operators and associativity is from left to right.
We can apply the above two rules to guess all
*ptr++ is treated as *(ptr++)
*++ptr is treated as *(++ptr)
++*ptr is treated as ++(*ptr)
Question 9 |
Output of following program
#include <stdio.h>
int fun(int arr[]) {
arr = arr+1;
printf("%d ", arr[0]);
}
int main(void) {
int arr[2] = {10, 20};
fun(arr);
printf("%d", arr[0]);
return 0;
}
Compiler Error | |
20 10 | |
20 20 | |
10 10 |
Discuss it
Question 9 Explanation:
In C, array parameters are treated as pointers (See http://www.geeksforgeeks.org/why-c-treats-array-parameters-as-pointers/ for details).
So the variable arr represents an array in main(), but a pointer in fun().
Question 10 |
What is printed by the following C program?
$include <stdio.h>
int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf( "%d", f(c,b,a));
getchar();
}
18 | |
19 | |
21 | |
22 |
Discuss it
Question 10 Explanation:
See Question 1 of http://www.geeksforgeeks.org/c-language-set-5/
There are 19 questions to complete.


