Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to answer this interview test about constant pointers?

Tags:

c

pointers

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;
like image 911
Registered User Avatar asked Jun 20 '11 05:06

Registered User


People also ask

What does a constant pointer mean?

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.

What is a pointer interview question?

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.

Which of the following is correct for pointer to constant?

int const* is pointer to constant integer This means that the variable being declared is a pointer, pointing to a constant integer.

Which is the correct way to declare a pointer to constant object?

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?


2 Answers

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)

like image 155
dragon135 Avatar answered Sep 23 '22 04:09

dragon135


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).

like image 35
James McNellis Avatar answered Sep 19 '22 04:09

James McNellis