Pointer Chain

Pointer Chain

Easy C Pointers 25 views
Explanation Complexity

Problem Statement

Create an integer variable. Create a pointer to it. Then create a pointer-to-pointer. Then create a triple pointer. Access and modify the original value from each level of the chain.

Input Format

No user input.
One integer variable is created in the program.

Output Format

Value of the integer after accessing and modifying it using:

• pointer

• pointer to pointer

• pointer to pointer to pointer

Example

Initial value:
x = 10
After modification using pointer: x = 20
After modification using double pointer: x = 30
After modification using triple pointer: x = 40

Constraints

• Integer variable only

• Pointers must be correctly linked

Concept Explanation

Each pointer level stores the address of the previous level.
Using dereferencing (*), the original variable can be accessed and changed.

Step-by-Step Explanation

1.Create an integer variable x and give it a value.

2.Create a pointer p that stores the address of x.

3.Create a pointer-to-pointer pp that stores the address of p.

4.Create a triple pointer ppp that stores the address of pp.

5.Modify x using *p.

6.Modify x using **pp.

7.Modify x using ***ppp.

8.Print the value of x after each modification.

Concept Explanation

Each pointer level stores the address of the previous level.
Using dereferencing (*), the original variable can be accessed and changed.

Step-by-Step Explanation

1.Create an integer variable x and give it a value.

2.Create a pointer p that stores the address of x.

3.Create a pointer-to-pointer pp that stores the address of p.

4.Create a triple pointer ppp that stores the address of pp.

5.Modify x using *p.

6.Modify x using **pp.

7.Modify x using ***ppp.

8.Print the value of x after each modification.

Input / Output Format

Input Format
No user input.
One integer variable is created in the program.
Output Format
Value of the integer after accessing and modifying it using:

• pointer

• pointer to pointer

• pointer to pointer to pointer
Constraints
• Integer variable only

• Pointers must be correctly linked

Examples

Input:
Initial value: x = 10
Output:
After modification using pointer: x = 20 After modification using double pointer: x = 30 After modification using triple pointer: x = 40

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int num = 10;           // Original integer variable
    int *ptr1 = &num;       // Pointer to int
    int **ptr2 = &ptr1;     // Pointer to pointer
    int ***ptr3 = &ptr2;    // Triple pointer

    printf("Original value: %dn", num);

    /* Modify via single pointer */
    *ptr1 = 20;
    printf("After *ptr1 = 20: %dn", num);

    /* Modify via pointer-to-pointer */
    **ptr2 = 30;
    printf("After **ptr2 = 30: %dn", num);

    /* Modify via triple pointer */
    ***ptr3 = 40;
    printf("After ***ptr3 = 40: %dn", num);

    /* Access via different levels */
    printf("nAccess values through pointers:n");
    printf("*ptr1  = %dn", *ptr1);
    printf("**ptr2 = %dn", **ptr2);
    printf("***ptr3= %dn", ***ptr3);

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int num = 10;           // Original integer variable
    int *ptr1 = &num;       // Pointer to int
    int **ptr2 = &ptr1;     // Pointer to pointer
    int ***ptr3 = &ptr2;    // Triple pointer

    printf("Original value: %dn", num);

    /* Modify via single pointer */
    *ptr1 = 20;
    printf("After *ptr1 = 20: %dn", num);

    /* Modify via pointer-to-pointer */
    **ptr2 = 30;
    printf("After **ptr2 = 30: %dn", num);

    /* Modify via triple pointer */
    ***ptr3 = 40;
    printf("After ***ptr3 = 40: %dn", num);

    /* Access via different levels */
    printf("nAccess values through pointers:n");
    printf("*ptr1  = %dn", *ptr1);
    printf("**ptr2 = %dn", **ptr2);
    printf("***ptr3= %dn", ***ptr3);

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

                                        
Please login to submit solutions.