Q2 which of the following statement(s) best refer to pointers?
A) Pointer arithmetic is permitted on pointers of any type.
B) A c pointer of type void can be used to directly examine or modify
any object of any type
C) A c pointer knows the types of pointers and indirectly referenced
data items at runtime
D) All of above
Can you please explain the options and why is option C correct?
Ques 2:
A. Option A is wrong, because we cannot perform pointers asthmatics with void pointers.
D. So, option D is also incorrect.
B. A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type.
nt a = 10;
char b = ‘x’;
void *p = &a; // void pointer holds address of int ‘a’
p = &b; // void pointer holds address of char ‘b’.
We cannot use it directly.
You are required to refer them to an object of some type before using them like:
void *p = &a;
Thus, this option is also wrong.
C. A c pointer knows the types of pointers and indirectly referenced data items at runtime.
We have already discussed that a must have a type before using it. So, it knows the type of data it can point to.
Also, the *p (referenced data item i.e. the value stored at the location pointed by it) is accessed at the time of execution or at the time , when we are using it.