C Pointers MCQs
C Pointers MCQs
C Pointers MCQs
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
A. nullp nullq
D. p q
C. 10
D. Undefined behaviour
void *vp;
int a = 100;
vp = &a;
printf("%d", *vp); //
wrong
It simply doesn't work that way!. Before you dereference a void pointer it must be typecasted to appropriate pointer type. Let me
show you what I mean.
For example: In the above snippet void pointer vp is pointing to the address of integer variable a. So in this case vp is acting as a
pointer to int or (int *). Hence the proper typecast in this case is (int*).
(int *)vptr
Now the type of vptr temporarily changes from void pointer to pointer to int or (int*) , and we already know how to dereference
a pointer to int, just precede it with indirection operator (*)
*(int *)vptr
Note: typecasting changes type of vp temporarily until the evaluation of the expression, everywhere else in the program vp is still a
void pointer.
5
#include<stdio.h>
#define SIZE 10
6
int main()
7
{
int i = 10;
8
float f = 2.34;
char ch = 'k';
9
1
void *vptr;
0
1
vptr = &i;
1
printf("Value of i = %d\n", *(int
1
*)vptr);
2
1
vptr = &f;
3
printf("Value of f = %.2f\n", *(float
1
*)vptr);
4
1
vptr = &ch;
5
printf("Value of ch = %c\n", *(char
1
*)vptr);
6
1
// signal to operating system program ran
7
fine
1
return 0;
8
}
1
9
2
0
2
1
2
2
2
3
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}
B. Undefined behaviour
C. 10
D. 0.000000
2. what is the output of this code
// function to pointer
#include<stdio.h>
int *f();
int main()
{
int *p = f();
printf("\n %d", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
A. &
B. *
C. ->
D. .
The unary indirection operator (*) accesses a value indirectly, through a pointer. The operand must be a pointer
type. The result of the operation is the value addressed by the operand.
int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
int x = 0;
void main()
{
Int x=10;
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
A. Same address
B. Different address
D. Varies
int x = 0;
void main()
{
int *const ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}
A. 0 1
C. 0xbfd605e8 0xbfd605ec
D. 0xbfd605e8 0xbfd605e8
void main()
{
int x = 0;
int *ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}
void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}
A. 5
B. Address of 5
C. Nothing
void main()
{
int x = 0;
int *ptr = &x;
printf("%d\n", *ptr);
}
A. Address of x
B. Junk value
C. 0
void func(int*);
int main()
{
int i = 10;
func((&i)++);
}
void func(int *p)
{
printf("%d\n", *p);
}
A. 10
void func(int*);
int main()
{
int i = 10, *p = &i;
func(p++);
}
void func(int *p)
{
printf("%d\n", *p);
}
A. 10
D. Segmentation fault
int main()
{
int i = 97, *p = &i;
func(&i);
printf("%d ", *p);
}
void func(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}
A. 2 97
B. 2 2
int main()
{
int i = 97, *p = &i;
func(&p);
printf("%d ", *p);
return 0;
}
void func(int **p)
{
int j = 2;
*p = &j;
printf("%d ", **p);
}
A. 2 2
B. 2 97
C. Undefined behaviour
int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;
printf("%d ", **p);
}
B. 10 10
C. Undefined behaviour
D. 10 11