Program 51 - CSU1128 - CSE 2026 - Shoolini University

Program to assign value of d entered by the user to value of m by pointers

Explanation

The program assigns the value of a variable d entered by the user to another variable m using pointers.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and two variables are declared:

  • int d: Variable to hold the value entered by the user.
  • int m: Variable to which the value of d will be assigned.
  • int *ptr: Pointer to hold the address of d.

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 d: ");: Prompts the user to enter the value of d.
  • scanf("%d", &d);: Reads the value entered by the user and stores it in d.

Pointer Assignment

The program uses a pointer to assign the value of d to m:

  • int *ptr = &d;: Declares a pointer ptr and initializes it with the address of d.
  • m = *ptr;: Dereferences the pointer and assigns the value at that address to m.

Output Result

The program prints the values of d and m:

  • printf("d = %d, m = %d\n", d, m);: Prints the values of d and m.

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 assign value of d entered by the user to value of m by pointers. \n\n");

    int d; // Declare and initialize an integer variable
    int m; // Declare another integer variable

    // Get value from user
    printf("Enter the value of d: ");
    scanf("%d", &d);

    // Use a pointer to assign the value of d to m
    int *ptr = &d; // Declare a pointer to d and initialize it with the address of d
    m = *ptr;      // Dereference the pointer and assign the value at that address to m

    printf("d = %d, m = %d\n", d, m); // Print the values of d and m

    return 0;
}

                

Output

Program to assign value of d entered by the user to value of m by pointers.

Enter the value of d: 5
d = 5, m = 5