Sie sind auf Seite 1von 11

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;
*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 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
WRONG
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

B4
800

D 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
CORRECT
#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;
}

25

B Garbage Value
C Compiler Error
D 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
CORRECT
#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

B MCQ Quiz Z CQ
C TEST Quiz Z CQ
D 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
CORRECT
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

B eeksQuiz
C Garbage Value
D 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
CORRECT
#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 %d\n", (*ptr)[1], (*ptr)[2]);

return 0;

2356

B 2345
C 4500
D none of the above
Discuss it

Question 7
WRONG
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("%s\n", t);
}

ab

B cd
C ef
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 8
CORRECT
#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 %d\n", (*ptr)[1], (*ptr)[2]);
return 0;
}

2356

B 2345
C 4500
D none of the above
Discuss it

Question 9
WRONG
#include <stdio.h>
int main(void)
{
int i;
int *ptr = (int *) malloc(5 * sizeof(int));
for (i=0; i<5; i++)
*(ptr + i) = i;
printf("%d
printf("%d
printf("%d
printf("%d
printf("%d

",
",
",
",
",

*ptr++);
(*ptr)++);
*ptr);
*++ptr);
++*ptr);

Compiler Error
01223

C 01234
D 12345
Discuss it

Question 9 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 10
WRONG
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

C 20 20
D 10 10

Discuss it

Question 10 Explanation:
In C, array parameters are treated as pointers (See http://www.geeksforgeeks.org/why-c-treatsarray-parameters-as-pointers/ for details). So the variable arr represents an array in main(), but a
pointer in fun().

Question 11
WRONG
What is printed by the following C program?
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

C 21
D 22
Discuss it

Question 11 Explanation:
See Question 1 of http://www.geeksforgeeks.org/c-language-set-5/

Question 12
CORRECT
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an
integer requires four bytes of memory.
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}, {10, 11, 12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}

2036, 2036, 2036

B 2012, 4, 2204
C 2036, 10, 10
D 2012, 4, 6
Discuss it

Question 13
CORRECT
Consider the following C program.
# include <stdio.h>
int main( )
{
static int a[] = {10, 20, 30, 40, 50};
static int *p[] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;
ptr++;
printf("%d%d", prt-p, **ptr};
}
The output of the program is _________

140
Discuss it

Question 13 Explanation:
The value of prt-p is 1 and value of **ptr is 40.

You have completed 13/13 questions .

Das könnte Ihnen auch gefallen