Q:

Modify value stored in other variable using pointer in C

belongs to collection: C pointers example programs

0

Modify value stored in other variable using pointer in C

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Steps:

  • Declare a pointer of same type
  • Initialize the pointer with the other (normal variable whose value we have to modify) variable's address
  • Update the value

Program:

#include <stdio.h>

int main(void) {
	int num = 10;
	//declaring and initializing the pointer
	int *ptr = &num;

	printf("value of num: %d\n", num);
	printf("value of num: (using pointer): %d\n", *ptr);

	//updating the value
	*ptr = 20;

	printf("value of num: %d\n", num);
	printf("value of num (using pointer): %d\n", *ptr);

	return 0;
}

Output

value of num: 10 
value of num: (using pointer): 10
value of num: 20 
value of num (using pointer): 20 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Making a valid pointer as NULL pointer in C...