Write a C program to create a pointer to an integer variable and display the value of the variable and its address.
Write a C program to create a pointer to an integer variable and display the value of the variable and its address.
A pointer stores the address of another variable. The address-of
operator & obtains the address of number,
and that address is stored in ptr.
Value = 25
Address = 0x7ff...
The exact address depends on the system.
Write a C program to access the value of a variable using a pointer
and the dereference operator *.
The dereference operator * accesses the value stored at
the memory address held by the pointer. Therefore, *ptr
gives the value of number.
Variable value = 50
Pointer value = 50
Write a C program to take an integer from the user and display its value using a pointer.
The pointer ptr stores the address of the input variable.
Dereferencing it with *ptr retrieves the variable's value.
Enter a number: 75
Value using pointer = 75
Write a C program to modify the value of a variable using a pointer.
A pointer can be used not only to read a variable but also to modify it.
Assigning a new value to *ptr changes the original variable.
Before modification = 10
After modification = 100
Write a C program to use pointers with two integer variables and calculate their sum.
Two pointers are used to access the values of two integer variables. The dereferenced values are then added to calculate the sum.
First number = 20
Second number = 30
Sum = 50
Write a C program to find the larger of two numbers using pointers.
The pointers provide access to the original variables, and their
dereferenced values are compared using an if-else statement.
Enter two numbers: 45 72
Larger number = 72
Write a C program to swap two numbers using pointers.
The addresses of a and b are passed to the
function. Because the function receives pointers, it can modify the
original variables directly.
Enter two numbers: 10 20
Before swap: a = 10, b = 20
After swap: a = 20, b = 10
Write a C program to use a pointer with a floating-point variable and display its value.
Pointers are not limited to integers. A pointer can store the address
of a variable of a compatible data type, such as float.
Price = 99.50
Price using pointer = 99.50
Write a C program to demonstrate that a pointer stores the address of a variable and can be used to access that variable.
The address stored in ptr points to the same memory
location as the address of number. Dereferencing the
pointer gives the value stored at that location.
Value of number = 100
Address of number = 0x7ff...
Address stored in ptr = 0x7ff...
Value using ptr = 100
The exact address depends on the system.
Write a C program to demonstrate a simple real-world style use of pointers by updating a student's marks through a pointer.
The pointer stores the address of the marks variable. By using
*ptr, the program directly updates the original marks.
This demonstrates why pointers are useful when a function or another
part of a program needs to modify existing data.
Original Marks = 75
Updated Marks = 85