Explanation
The program demonstrates the use of pointers by allowing the user to enter a value for a variable and then modifying that value using a pointer.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Variable Declaration
The main()
function is defined, and an integer variable and a pointer to that variable are declared:
int d;
: Variable to hold the value entered by the user.int *m = &d;
: Pointer to hold the address ofd
and initialized with the address ofd
.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the value of d
:
printf("Enter the value of X: ");
: Prompts the user to enter the value ofd
.scanf("%d", &d);
: Reads the value entered by the user and stores it ind
.
Pointer Demonstration
The program demonstrates the use of pointers by printing the values and addresses:
printf("d = %d\n", d);
: Prints the value ofd
.printf("&d = %p\n", &d);
: Prints the memory address ofd
.printf("m = %p\n", m);
: Prints the value of the pointerm
(address ofd
).printf("*m = %d\n", *m);
: Prints the value stored at the memory address pointed to bym
.
Modify Value Using Pointer
The program modifies the value of d
using the pointer m
:
printf("Enter the new value of *m pointer to modify the value of X: ");
: Prompts the user to enter a new value using the pointer.scanf("%d", &*m);
: Reads the new value and stores it in the memory address pointed to bym
.
Output Modified Value
The program prints the modified value of d
:
printf("d = %d\n", d);
: Prints the modified value ofd
.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
int main()
{
printf("\n\n Program to demonstrate the pointers. \n\n");
// Declare an integer variable and a pointer to that variable
int d;
int *m = &d;
// Get input for the value of X
printf("Enter the value of X: ");
scanf("%d", &d);
// Print the value of d and the memory address of d
printf("d = %d\n", d);
printf("&d = %p\n", &d);
// Print the value of m and the value stored at the memory address pointed to by m
printf("m = %p\n", m);
printf("*m = %d\n", *m);
// Modify the value of d using the pointer
printf("Enter the new value of *m pointer to modify the value of X: ");
scanf("%d", &*m);
// Print the modified value of d
printf("d = %d\n", d);
return 0;
}
Output
Program to demonstrate the pointers.
Enter the value of X: 6
d = 6
&d = 0x7ffc22169b88
m = 0x7ffc22169b88
*m = 6
Enter the new value of *m pointer to modify the value of X: 3
d = 3