Lesson 45 of 60 – Pointers in C
75%

Pointers in C

A pointer is a variable that stores the memory address of another variable. Pointers are one of the most important features of C because they allow programs to work directly with memory, arrays, functions, structures, and dynamic memory.

Note: The & operator obtains the address of a variable, while the * operator is used to declare a pointer and to access the value stored at the address held by a pointer.

1. What is a Pointer?

A pointer is a variable that stores the memory address of another variable.

int age = 20;
int *ptr = &age;

Here, ptr stores the address of age.

2. Why Do We Use Pointers?

Pointers are useful for:

  • Accessing memory addresses
  • Changing variables through their addresses
  • Working with arrays
  • Passing data to functions
  • Dynamic memory allocation
  • Working with structures
  • Building data structures

3. Address of a Variable

Every variable is stored at a location in memory. The & operator can be used to obtain its address.

int number = 50;

printf("%p", (void *)&number);

The %p format specifier is used to display a pointer value.

4. Declaring a Pointer

A pointer is declared using the * symbol.

int *ptr;
float *pricePtr;
char *charPtr;

The pointer type indicates the type of object that the pointer is intended to point to.

5. Assigning an Address to a Pointer

Use the address-of operator & to store the address of a variable in a pointer.

int number = 100;
int *ptr = &number;

Now ptr points to number.

6. Dereferencing a Pointer

Dereferencing means accessing the value stored at the address held by a pointer. The * operator is used for this purpose.

int number = 100;
int *ptr = &number;

printf("%d", *ptr);

The output is:

100

7. Changing a Value Using a Pointer

A pointer can be used to modify the value of the variable it points to.

int number = 10;
int *ptr = &number;

*ptr = 50;

printf("%d", number);

The value of number becomes 50.

8. Pointer Example

#include <stdio.h>

int main()
{
    int number = 25;
    int *ptr = &number;

    printf("Value = %d\n", number);
    printf("Address = %p\n", (void *)&number);
    printf("Pointer Value = %p\n", (void *)ptr);
    printf("Value through Pointer = %d\n", *ptr);

    return 0;
}

9. Pointer and Data Type

The pointer type should normally match the type of object it points to.

int number = 10;
int *ptr = &number;

float price = 25.5f;
float *pricePtr = &price;

char grade = 'A';
char *gradePtr = &grade;

10. Pointer and Address

A pointer contains an address, while dereferencing the pointer accesses the object stored at that address.

int x = 40;
int *p = &x;

printf("%p\n", (void *)p);
printf("%d\n", *p);

The first statement displays an address and the second displays the value.

11. NULL Pointer

A null pointer does not point to a valid object.

int *ptr = NULL;

Before dereferencing a pointer that may be null, check it first.

if(ptr != NULL)
{
    printf("%d", *ptr);
}

12. Pointer Size

The size of a pointer can be found using sizeof.

int *ptr;

printf("%zu", sizeof(ptr));

Pointer size depends on the target platform and implementation. Different pointer types may have different sizes on some systems.

13. Multiple Pointers

Different pointers can point to different variables.

int a = 10;
int b = 20;

int *p1 = &a;
int *p2 = &b;

printf("%d\n", *p1);
printf("%d\n", *p2);

14. Pointer Assignment

A pointer can be assigned the address stored in another compatible pointer.

int number = 50;

int *p1 = &number;
int *p2 = p1;

printf("%d", *p2);

Both pointers point to the same object.

15. Pointer Arithmetic

Pointer arithmetic is especially useful when working with arrays.

int numbers[] = {10, 20, 30};

int *ptr = numbers;

printf("%d\n", *ptr);

ptr++;

printf("%d\n", *ptr);

After incrementing, ptr points to the next array element.

16. Pointer Increment

When a pointer to an array element is incremented, it advances to the next element of the pointed-to type.

int arr[] = {10, 20, 30};

int *p = arr;

p++;

printf("%d", *p);

The output is 20.

17. Pointer Decrement

A pointer can also be decremented to move toward the previous element of an array.

int arr[] = {10, 20, 30};

int *p = &arr[2];

p--;

printf("%d", *p);

The output is 20.

18. Pointer and Array

The name of an array generally converts to a pointer to its first element when used in an expression.

int numbers[] = {10, 20, 30};

int *ptr = numbers;

printf("%d", *ptr);

The output is 10.

19. Accessing Array Elements with a Pointer

Array elements can be accessed using pointer arithmetic.

int numbers[] = {10, 20, 30, 40};

int *ptr = numbers;

printf("%d\n", *(ptr + 0));
printf("%d\n", *(ptr + 1));
printf("%d\n", *(ptr + 2));
printf("%d\n", *(ptr + 3));

20. Difference Between & and *

The & and * operators have different purposes in pointer operations.

int number = 100;
int *ptr = &number;
  • &number → address of number
  • ptr → stores that address
  • *ptr → value stored at that address

21. Pointer with scanf()

The scanf() function uses addresses to store input in variables.

int age;

printf("Enter age: ");
scanf("%d", &age);

Here, &age gives scanf() the address where the input value should be stored.

22. Pointer as a Function Argument

A pointer can be passed to a function when the function needs to modify the original variable.

void change(int *p)
{
    *p = 100;
}

int main()
{
    int number = 10;

    change(&number);

    printf("%d", number);

    return 0;
}

23. Changing Two Values with Pointers

A function can receive multiple pointers.

void change(int *a, int *b)
{
    *a = 50;
    *b = 100;
}

int main()
{
    int x = 10;
    int y = 20;

    change(&x, &y);

    printf("%d %d", x, y);

    return 0;
}

24. Pointer to Pointer

A pointer can also store the address of another pointer. This is called a pointer to pointer.

int number = 50;

int *p = &number;
int **pp = &p;

printf("%d", **pp);

Here, pp points to p, and **pp accesses the value of number.

25. Pointer to Character

Pointers can point to characters as well.

char grade = 'A';

char *ptr = &grade;

printf("%c", *ptr);

The pointer type should match the type of the object it points to.

26. Common Pointer Mistake: Uninitialized Pointer

Do not dereference an uninitialized pointer.

int *ptr;

*ptr = 10;

The pointer does not point to a valid object here. A pointer should first be assigned a valid address or set to NULL before it is used appropriately.

27. Common Pointer Mistake: Invalid Dereference

Dereferencing a null pointer is invalid.

int *ptr = NULL;

printf("%d", *ptr);

Always ensure that a pointer points to a valid object before dereferencing it.

28. Complete Pointer Program

#include <stdio.h>

int main()
{
    int number = 25;
    int *ptr = &number;

    printf("Value = %d\n", number);
    printf("Address = %p\n", (void *)&number);
    printf("Pointer = %p\n", (void *)ptr);
    printf("Value using pointer = %d\n", *ptr);

    *ptr = 75;

    printf("New value = %d\n", number);

    return 0;
}

This program demonstrates address access, pointer storage, dereferencing, and changing a variable through a pointer.

29. Practical Uses of Pointers

Pointers are used in many important areas of C programming.

  • Passing variables to functions for modification
  • Working with arrays and strings
  • Dynamic memory allocation
  • Structures and linked data structures
  • Efficient manipulation of data
  • System programming
  • Working with memory addresses
  • Building linked lists, stacks, and queues

30. Practice Program

Create a C program that:

  1. Creates an integer variable.
  2. Creates a pointer to that variable.
  3. Displays the variable value.
  4. Displays its address.
  5. Displays the value using the pointer.
  6. Changes the value using the pointer.
  7. Displays the updated value.

Then create an array and use a pointer to display all of its elements.

📌 Key Points

  • A pointer stores the address of another object.
  • & obtains the address of an object.
  • * is used to declare a pointer and dereference it.
  • Dereferencing accesses the value stored at the pointed-to address.
  • NULL represents a null pointer value.
  • Pointers are closely related to arrays.
  • Pointer arithmetic is useful when working with arrays.
  • Pointers can be passed to functions to modify original variables.
  • A pointer can point to another pointer.
  • Always initialize pointers appropriately before using them.
  • Never dereference an invalid or null pointer.
  • Use %p with a (void *) conversion when displaying a pointer value with printf().

🧠 Quick Quiz

Question: Which operator is used to access the value stored at the address held by a pointer?