C Programming • Pointers & File handling
C Programming / Use of Pointers

Use of Pointers

Notes 5 Pointers & File handling

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.

Notes

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.

💡 Example
int number = 10; int *ptr = &number; *ptr = 20;

The pointer accesses the original variable, so the value of number becomes 20.

1. Accessing a Variable Through a Pointer

A pointer can be used to access the value of the variable whose address it stores. The indirection operator * is used for this purpose.

💡 Example
int marks = 75; int *ptr = &marks; printf("%d", *ptr);

*ptr accesses the value stored in marks, so the output is 75.

2. Modifying a Variable Through a Pointer

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.

💡 Example
int number = 10; int *ptr = &number; *ptr = 50;

After *ptr = 50, the original variable number also contains 50.

3. Passing an Address to a Function

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.

💡 Example
void change(int *p) { *p = 100; } change(&number);

The function receives the address of number and changes its original value through *p.

4. Pointers and Call by Reference

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.

💡 Example
swap(&a, &b);

The addresses of a and b are passed to the function so their original values can be exchanged.

5. Using Pointers with Arrays

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.

💡 Example
int marks[3] = {70, 80, 90}; int *ptr = marks; printf("%d", *ptr);

The pointer points to the first element of the array, so *ptr gives 70.

6. Pointer Arithmetic

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.

💡 Example
int arr[3] = {10, 20, 30}; int *ptr = arr; ptr++;

After ptr++, the pointer points to the next integer element, which is arr[1].

Pointer Increment Depends on Data Type

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.

💡 Example
int *p; char *c; float *f;

When these pointers are incremented, each one moves to the next element of its own data type.

7. Comparing Pointer Values

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.

💡 Example
if (ptr == NULL) { printf("Pointer is empty"); }

This checks whether ptr contains a null pointer value.

8. Using Pointers to Work Efficiently with Data

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.

💡 Example

An array can be processed by passing its address to a function, allowing the function to work with the array elements directly.

Practical Example

Problem Statement

Write a C program to demonstrate how a pointer can be used to modify the value of an original variable.

Learning Outcomes

  • Use a pointer to access a variable.
  • Modify an original variable through a pointer.
  • Understand the relationship between a pointer and its target variable.

Hint

Declare an integer variable, store its address in a pointer and assign a new value using the indirection operator.

Theory

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.

Program

#include <stdio.h> int main() { int number = 25; int *ptr; // store the address of number in the pointer ptr = &number; printf("Before modification = %d ", number); // modify the original variable through the pointer *ptr = 50; printf("After modification = %d ", number); return 0; }

Expected Output

Before modification = 25

After modification = 50

Note

The pointer ptr stores the address of number. Therefore, changing *ptr changes the original variable number.

Practical Example — Function and Pointer

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.

💡 Example
void changeValue(int *p) { *p = 100; }

The pointer parameter p receives the address of the original variable.

Problem Statement

Write a C program to pass the address of a variable to a function and change its value using a pointer.

Program

#include <stdio.h> void changeValue(int *p) { // change the original value using its address *p = 100; } int main() { int number = 25; printf("Before function call = %d ", number); // pass the address of number to the function changeValue(&number); printf("After function call = %d ", number); return 0; }

Expected Output

Before function call = 25

After function call = 100

Note

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.

Quick Revision

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.

Important Exam Questions

Short Answer Questions

  1. What are the main uses of pointers in C?
  2. How can a pointer modify the value of a variable?
  3. How are pointers used with functions?
  4. How are pointers related to arrays?
  5. What is pointer arithmetic?
  6. What is the purpose of a NULL pointer?

Long Answer Questions

  1. Explain the different uses of pointers in C with suitable examples.
  2. Explain how pointers can be used to modify the value of an original variable.
  3. Explain the use of pointers in functions with a suitable program.
  4. Explain pointer arithmetic with a suitable example.
🎥 Recommended Learning

Watch a beginner-friendly explanation of the uses of pointers in C programming.

▶ Watch: Uses of Pointers in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for uses of pointers will be provided here.

🧠 Mind Map

Use the mind map for value access, modification, functions, arrays and pointer arithmetic.