Important Question Related to const pointer in C

C interview Question related to const pointer

Are you preparing for your new JOB?

If your answer is “Yes“, you are in luck! In this blog post, we will discuss some important interview questions related to const pointer in C which might ask by your interviewer.

In my previous article, I have already discussed the const qualifier and its functionality. If you have some confusion regarding the const in C or you are not familiar with the const qualifier then it’s my personal advice to you, first, you have to read about the const keyword before solving the below C interview question related to const pointer.

Prerequisite Question Related to Const Pointer:

 

In this article, I will only discuss some important questions related to the const pointer which is frequently asked by an interviewer in the interview. I have spent many hours collecting these const pointer questions so I hope these questions will be helpful for you. If you have any doubts regarding these pointer const questions, then please write in the comment box. I will try to solve your problem.

Note: All const pointer programs are tested on the dev C++.

List of a few const pointer questions:

Question 1:

#include <stdio.h>
#include <stdlib.h>

int main()
{

    const int *ciData = malloc(1*sizeof (int));
    *ciData=7; //assign a value

    return 0;
}

Output:

[Error] assignment of read-only location ‘*ciData’

Explanation:

ciData is the pointer to the constant integer, so we cannot change the value of the address which holds by ciData.

 

Question 2:

#include <stdio.h>

int main()
{

    char *pchar="aticle";

    pchar[1]='d';

    printf("%c",pchar[1]);

    return 0;
}

Output:

Error:  Runtime error

Explanation

The “aticle” is a string literal, so when trying to compile the above program compiler does not throw the error but when you try to run the program. it will be the crash.

 

Question 3:

#include <stdio.h>

int main()
{
    const char *pcName="aticleworld";

    pcName++;
    printf("%s", ++pcName);

    return 0;
}

Output:

icleworld

Explanation:

In the above example, pcName points to the constant character but itself is not constant. So we can increment the position of the pointer.

 

Question 4:

#include <stdio.h>

int main()
{
    const char *pcName="aticleworld";

    pcName[0] = 'A' ;
    printf("%s", pcName);

    return 0;
}

Output:

[Error] assignment of read-only location ‘*pcName’

Explanation:

Here pointer points to string literals, so we cannot replace the character.

 

Question 5:

#include <stdio.h>

int main()
{
    char * const pcName="aticleworld";

    pcName++;
    printf("%s",pcName);

    return 0;
}

Output:

[Error] increment of read-only variable ‘pcName’

Explanation:

In the above example, the pointer is itself constant. As we know we cannot increment the constant pointer, so it is the reason for getting errors.

 

Question 6:

#include <stdio.h>

int main()
{
    const char *const pcName="aticleworld";

    pcName++;
    printf("%s",pcName);

    return 0;
}

Output:

[Error] increment of read-only variable ‘pcName’

Explanation

In the above example, the pointer is constant and it also points to a constant address.

 

Question 7:

#include <stdio.h>

int main()
{
    const int ciData = 5;
    int * piData = NULL;

    printf("Value before the change = %d\n\n",ciData);

    //assign the constant address to the pointer
    piData = (int*)&ciData;

    *piData = 6;
    printf("Value after the change = %d\n\n",ciData);

    return 0;
}

OutPut:

Undefined Behaviour (UB)

Explanation:

According to C standard behavior of the above code will be undefined. The output may be different on different machines.

 

Question 8:

#include <stdio.h>

int main()
{
    const int a =7;
    const int  *p=&a;

    printf("%d",*p++);

    return 0;
}

Output:

7

Explanation

The precedence of postfix ++ is higher than asterisk ( * ). So  *p++ will be equivalent to *(p++). In the above example, the pointer is not constant only pointed data is constant.

 

Question 9:

#include <stdio.h>

int main()
{
    const int a =7;
    const int  * p=&a;

    printf("%d",*++p);

    return 0;
}

Output:

GarbageData

Explanation
In the above example, the indirection operator and pre-increment operator have the same priority level and associative is left-right. So *++p behaves like *(++p)  that is why now p point to a garbage data location.

 

Question 10:

#include <stdio.h>

int main()
{
    const int a =7;
    const int  * p=&a;

    printf("%d",++*p);

    return 0;
}

Output:

[Error] increment of read-only location ‘*p’

Explanation

In the above example, the indirection operator and pre-increment operator have the same priority level and associative is left-right. Here ++*p behaves like ++(*p), so when to compile this code compiler throws a compilation error because pointer p points to a read-only address.

 

Question 11:

#include <stdio.h>

union Information
{
    char acName[15];
    int iAge;
};

const union Information Info1;

int main()
{
    Info1.iAge =24;

    printf("%d",Info1.iAge);

    return 0;
}

Output:

[Error] assignment of member ‘iAge’ in read-only object

Explanation:

In the above example, when we have declared the union variable constant then all members of the union become constant associated to that object.

 

Question 12:

#include<stdio.h>
#include <string.h>

union Information
{
    char acName[15];
    int iAge;
};
const union Information Info1;

int main()
{
    strcpy(Info1.acName, "aticleworld");
    printf("%s", Info1.acName);

    return 0;
}

OutPut:

Undefined Behaviour

Explanation:

Trying to modify the object defined with a const-qualified type through the use of an lvalue with a non-const-qualified type.

 

Question 13:

#include<stdio.h>

int ReplaceValue(int *iData)
{
    *iData = 11;
    return 0;
}
int main()
{
    const int arr[5] = {1, 2, 3, 4, 5};
    
    printf("arr[3] before calling function = %d\n\n", arr[3]);
    ReplaceValue(&arr[3]);
    printf("\narr[3] after calling  function = %d\n\n", arr[3]);
    
    return 0;
}

Output:

Undefined Behaviour (UB)

 

Recommended Post:

7 comments

  1. int main() {

    const int a =7;
    const int * p=&a;

    printf(“%d\n”,*p++); // why does this not throw error as we are trying to modify value of *p

    printf(“%d”,*p); // adding this line throws any random value as result. can u explain why
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *