Const Challenge

Const Challenge

Hard C Variables 40 views
Explanation Complexity

Problem Statement

Const int, const pointer, pointer to const - declare all three. Then try modifying them and see which modification is allowed and which one gives a compile error.

Input Format

Declarations of:

• const int

• const pointer

• pointer to const

Output Format

Which modification is allowed and which gives compile error.

Example

const int a = 10;

int x = 20;
int y = 30;
int *const p = &x;

const int b = 40;
const int *q = &b;
a = cannot be modified
p = cannot change address
*q = cannot modify value

Constraints

• C language rules

• Compile-time checking

Concept Explanation

const means cannot be changed.
Where const is placed decides what is fixed.

Step-by-Step Explanation

1. const int

const int a = 10;

• a = 20; compile error

• Reason: value constant hai

2. const pointer (pointer is constant)

int *const p = &x;

• *p = 50; allowed

• p = &y; compile error

• Pointer address is fixed, value can change.

3. pointer to const

const int *q = &b;

• *q = 60; error

• q = &x; allowed

• Value is constant, pointer can change.

Concept Explanation

const means cannot be changed.
Where const is placed decides what is fixed.

Step-by-Step Explanation

1. const int

const int a = 10;

• a = 20; compile error

• Reason: value constant hai

2. const pointer (pointer is constant)

int *const p = &x;

• *p = 50; allowed

• p = &y; compile error

• Pointer address is fixed, value can change.

3. pointer to const

const int *q = &b;

• *q = 60; error

• q = &x; allowed

• Value is constant, pointer can change.

Input / Output Format

Input Format
Declarations of:

• const int

• const pointer

• pointer to const
Output Format
Which modification is allowed and which gives compile error.
Constraints
• C language rules

• Compile-time checking

Examples

Input:
const int a = 10; int x = 20; int y = 30; int *const p = &x; const int b = 40; const int *q = &b;
Output:
a = cannot be modified p = cannot change address *q = cannot modify value

Example Solution (Public)

C
#include <stdio.h>

int main() {

    /* 1. const int */
    const int a = 10;
    // a = 20;   // ❌ ERROR: cannot modify const int

    /* 2. pointer to const int */
    int x = 5, y = 15;
    const int *p = &x;
    // *p = 10;  // ❌ ERROR: cannot modify value through pointer
    p = &y;      // ✅ ALLOWED: pointer can change

    /* 3. const pointer to int */
    int z = 25;
    int *const q = &z;
    *q = 30;     // ✅ ALLOWED: value can change
    // q = &x;   // ❌ ERROR: pointer cannot change

    printf("a = %dn", a);
    printf("Value pointed by p = %dn", *p);
    printf("Value pointed by q = %dn", *q);

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {

    /* 1. const int */
    const int a = 10;
    // a = 20;   // ❌ ERROR: cannot modify const int

    /* 2. pointer to const int */
    int x = 5, y = 15;
    const int *p = &x;
    // *p = 10;  // ❌ ERROR: cannot modify value through pointer
    p = &y;      // ✅ ALLOWED: pointer can change

    /* 3. const pointer to int */
    int z = 25;
    int *const q = &z;
    *q = 30;     // ✅ ALLOWED: value can change
    // q = &x;   // ❌ ERROR: pointer cannot change

    printf("a = %dn", a);
    printf("Value pointed by p = %dn", *p);
    printf("Value pointed by q = %dn", *q);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.