Pointers are used when a program needs to work with the address of a variable instead of using only its value. Through a pointer, a program can access and modify the original variable, pass its address to a function, and work efficiently with arrays and other data structures.
Pointers are used when a program needs to work with the address of a variable instead of using only its value. Through a pointer, a program can access and modify the original variable, pass its address to a function, and work efficiently with arrays and other data structures.
The pointer accesses the original variable, so the value of
number becomes 20.
A pointer can be used to access the value of the variable whose
address it stores. The indirection operator * is used
for this purpose.
*ptr accesses the value stored in
marks, so the output is 75.
A pointer does not only allow us to read a value. It can also be used to change the value of the original variable by assigning a new value through the indirection operator.
After *ptr = 50, the original variable
number also contains 50.
A pointer allows the address of a variable to be passed to a function. The function can then use that address to access the original variable instead of receiving only a separate copy of its value.
The function receives the address of number and
changes its original value through *p.
In C, pointers are commonly used to achieve the effect of call by reference. Instead of passing a copy of the value, the address of the variable is passed to the function. The function can then modify the original variable.
The addresses of a and b are passed to
the function so their original values can be exchanged.
The address of an array element can be stored in a pointer. Pointers can then be used to access array elements. This becomes especially useful when processing an array using functions.
The pointer points to the first element of the array, so
*ptr gives 70.
Pointers can be incremented and decremented to move through consecutive elements of an array. When a pointer is increased by one, it moves to the next element of the type it points to.
After ptr++, the pointer points to the next integer
element, which is arr[1].
Pointer arithmetic is based on the size of the data type to which the pointer points. Therefore, incrementing a pointer moves it to the next element rather than simply adding one byte to the address.
When these pointers are incremented, each one moves to the next element of its own data type.
Pointer variables can be compared when they point to compatible
objects. Comparing pointers is useful when working with elements
of the same array or when checking whether a pointer is
NULL.
This checks whether ptr contains a null pointer
value.
Pointers are useful when a function needs to work with the original data rather than making another copy of that data. They are therefore widely used with arrays, functions, structures and dynamic memory.
An array can be processed by passing its address to a function, allowing the function to work with the array elements directly.
Write a C program to demonstrate how a pointer can be used to modify the value of an original variable.
Declare an integer variable, store its address in a pointer and assign a new value using the indirection operator.
When a pointer contains the address of a variable, the expression
*ptr refers to the value stored at that address.
Assigning a new value to *ptr therefore changes the
original variable.
Before modification = 25
After modification = 50
The pointer ptr stores the address of
number. Therefore, changing *ptr
changes the original variable number.
One of the important uses of pointers is passing the address of a variable to a function so that the function can modify the original value.
The pointer parameter p receives the address of the
original variable.
Write a C program to pass the address of a variable to a function and change its value using a pointer.
Before function call = 25
After function call = 100
The function receives the address of number, so
changing *p changes the original variable. This is
the basic idea behind using pointers for call by reference.
| Use | Remember |
|---|---|
| Access Value | Use *ptr. |
| Modify Value | Assign through *ptr. |
| Function | Pass an address using a pointer. |
| Arrays | Pointer can move through array elements. |
| Pointer Arithmetic | Increment moves to the next element of its type. |
| NULL | Can be used to represent a pointer that points to no valid object. |
Watch a beginner-friendly explanation of the uses of pointers in C programming.
A short handwritten-style revision sheet for uses of pointers will be provided here.
Use the mind map for value access, modification, functions, arrays and pointer arithmetic.