C Changing Value Pointed by Pointers
#include <stdio.h>
void changeValue(int *ptr) {
*ptr = 20;
}
int main() {
int num = 10;
int *ptr = #
printf("Before change: %d\n", *ptr);
changeValue(ptr);
printf("After change: %d\n", *ptr);
return 0;
}
Explanation:
1. Include the necessary header file stdio.h
.
2. Declare a function changeValue
that takes a pointer to an integer as its argument.
3. Inside the changeValue
function, dereference the pointer ptr
and assign the value 20 to the integer it points to.
4. Define the main
function.
5. Declare an integer variable num
and set its initial value to 10.
6. Declare a pointer ptr
and initialize it with the memory address of num
.
7. Print the value pointed to by ptr
before any changes using printf
.
8. Call the changeValue
function, passing the address of num
(via ptr
).
9. Print the value pointed to by ptr
after the change made by the changeValue
function.
10. Return 0, indicating successful program execution.