I had an interview in which they had asked me this question
#include<stdio.h>
int main ()
{
int* const p=NULL;
int const *q=NULL;
p++;
q++;
printf("%d\n",p);
printf("%d\n",q);
}
How will above program behave
a) p will increment 4 bytes;
and q will also increment 4 bytes;
b) p will be zero
q will point to memory 4 bytes ahead;
c) error will come in above program
I am not able to understand what is the difference between the statements
int* const p=NULL;
int const *q=NULL;
A constant pointer is one that cannot change the address it contains. In other words, we can say that once a constant pointer points to a variable, it cannot point to any other variable. Note: However, these pointers can change the value of the variable they point to but cannot change the address they are holding.
1. What is a pointer? Ans: This is said to be a very basic pointer interview question. Pointer is a variable that stores the address of another variable. The syntax of a pointer is represented as.
int const* is pointer to constant integer This means that the variable being declared is a pointer, pointing to a constant integer.
Explanation: int *ptr is the correct way to declare a pointer. 2. Which of the following gives the [value] stored at the address pointed to by the pointer : ptr?
int* const p=NULL;
p
is a constant-pointer to an integer. The pointer IS constant (the pointer value cannot be changed); the integer pointed to is not constant (the integer value can be modified).
So statement:
p++;
will fail to compile because trying to modify a constant value (the pointer).
and statement:
(*p)++;
will increment the integer value being pointed by pointer p
(but because p
is assigned NULL
, it will be undefined behaviour)
int const *q=NULL;
q
is a pointer to a constant-integer.The pointer is not constant (the pointer value can be changed); the integer pointed to IS constant (the integer value cannot be modified).
So statement:
q++;
will modify pointer q
to point to memory 4 bytes ahead (assuming sizeof(int)
is 4). (because q
is assigned NULL
, q
will be 0x4
-- I assume NULL is zero (which is true in all current implementation), incrementing NULL pointer is actually undefined behaviour )
and statement:
(*q)++;
will fail to compile because trying to modify a constant value (the integer pointed to is a constant)
How will above program behave?
This is rather simple to answer: the program will not compile.
The postfix ++
requires a modifiable lvalue as its argument; p
is not modifiable because it is const
-qualified.
The const
after the *
means that the pointer is qualified; if the const
appears before the *
as it does in the declaration of q
, it means that the object referred to by the pointer is qualified. You can decode the C declarator syntax using the clockwise/spiral rule.
If you remove p
and all references to it from the program so that only the lines containing q
remain, the answer is that the program exhibits undefined behavior: you cannot perform arithmetic on a null pointer (at least not if the result is not the null pointer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With