1.8 Pointers in C Programming

Module 1.8 • Memory References, Dereferencing, Pass-by-Reference & Allocations

1.8.1 Introduction to Pointers

A pointer is a variable that stores the memory address of another variable. Instead of storing actual data, a pointer stores the location where data is kept in memory.

Pointers are one of the most powerful features of C because they enable:

1.8.2 Understanding Memory Addresses

Every variable occupies some location in memory.

Example:

int marks = 85;

Assume the variable is stored at address:

Address: 2000
Value  : 85

Memory representation:

Address Value
200085

To obtain this address, C provides the address operator &.

printf("%p", &marks);

1.8.3 Address Operator (&)

The & operator returns the memory address of a variable.

Example Program

#include <stdio.h>
 
int main()
{
    int age = 28;
 
    printf("Value = %d\n", age);
    printf("Address = %p\n", &age);
 
    return 0;
}

Sample Output

Value = 28
Address = 0x61ff08

The address varies from system to system.

1.8.4 Declaring Pointer Variables

Syntax

data_type *pointer_name;

Examples

int *ptr;
char *ch;
float *fp;
double *dp;

Meaning:

Pointer Stores Address Of
int *Integer variable
char *Character variable
float *Float variable
double *Double variable

1.8.5 Initializing a Pointer

A pointer should store the address of a variable.

Example

int number = 75;
int *ptr = &number;

Memory Diagram

number = 75
 
Address of number = 3000
 
ptr = 3000

Pointer ptr points to number.

1.8.6 Dereferencing Operator (*)

The * operator accesses the value stored at the address contained in a pointer.

Example Program

#include <stdio.h>
 
int main()
{
    int salary = 45000;
    int *ptr = &salary;
 
    printf("Value of salary = %d\n", salary);
    printf("Using pointer = %d\n", *ptr);
 
    return 0;
}

Output

Value of salary = 45000
Using pointer = 45000

1.8.7 Pointer Demonstration

Example Program

#include <stdio.h>
 
int main()
{
    int score = 92;
    int *ptr = &score;
 
    printf("Address of score = %p\n", &score);
    printf("Value of score = %d\n", score);
    printf("Value stored in ptr = %p\n", ptr);
    printf("Value through ptr = %d\n", *ptr);
 
    return 0;
}

Output

Address of score = 0x61ff20
Value of score = 92
Value stored in ptr = 0x61ff20
Value through ptr = 92

1.8.8 Changing Values Through Pointers

Pointers can modify variables directly.

Example Program

#include <stdio.h>
 
int main()
{
    int value = 50;
    int *ptr = &value;
 
    *ptr = 120;
 
    printf("%d", value);
 
    return 0;
}

Output

120

The original variable changes because the pointer accesses the same memory location.

1.8.9 NULL Pointer

A NULL pointer does not point to any valid memory location.

Example

int *ptr = NULL;

Benefits:

1.8.10 Pointer Arithmetic

Pointers can move through memory.

Valid operations:

ptr++
ptr--
ptr + n
ptr - n

Example

int values[4] = {10,20,30,40};
int *ptr = values;

Initially:

ptr → 10

After:

ptr++;

Pointer moves to:

ptr → 20

1.8.11 Pointer Arithmetic Example

Example Program

#include <stdio.h>
 
int main()
{
    int nums[] = {5,10,15,20};
 
    int *ptr = nums;
 
    printf("%d\n", *ptr);
 
    ptr++;
 
    printf("%d\n", *ptr);
 
    return 0;
}

Output

5
10

1.8.12 Relationship Between Arrays and Pointers

Array name represents the address of the first element.

Example

int arr[] = {12,24,36,48};

The following are equivalent:

arr
&arr[0]

Both point to the first element.

1.8.13 Accessing Array Elements Using Pointers

Example Program

#include <stdio.h>
 
int main()
{
    int arr[] = {3,6,9,12,15};
    int *ptr = arr;
 
    int i;
 
    for(i=0;i<5;i++)
    {
        printf("%d ", *(ptr+i));
    }
 
    return 0;
}

Output

3 6 9 12 15

1.8.14 Pointer Subscript Notation

Pointers can use array indexing.

Example

int arr[] = {100,200,300};
 
int *ptr = arr;

Valid:

ptr[0]
ptr[1]
ptr[2]

Output:

100
200
300

1.8.15 Passing Pointers to Functions

Pointers allow functions to modify original variables.

Example Program

#include <stdio.h>
 
void update(int *p)
{
    *p = *p + 25;
}
 
int main()
{
    int num = 75;
 
    update(&num);
 
    printf("%d", num);
 
    return 0;
}

Output

100

1.8.16 Call by Value

Example Program

#include <stdio.h>
 
void change(int x)
{
    x = 500;
}
 
int main()
{
    int value = 100;
 
    change(value);
 
    printf("%d", value);
 
    return 0;
}

Output

100

Original variable remains unchanged.

1.8.17 Simulating Call by Reference

Example Program

#include <stdio.h>
 
void change(int *x)
{
    *x = 500;
}
 
int main()
{
    int value = 100;
 
    change(&value);
 
    printf("%d", value);
 
    return 0;
}

Output

500

1.8.18 Returning Multiple Values Using Pointers

Example Program

#include <stdio.h>
 
void calculate(int a,int b,int *sum,int *product)
{
    *sum = a + b;
    *product = a * b;
}
 
int main()
{
    int s,p;
 
    calculate(8,4,&s,&p);
 
    printf("Sum = %d\n", s);
    printf("Product = %d\n", p);
 
    return 0;
}

Output

Sum = 12
Product = 32

1.8.19 Dynamic Memory Allocation

Memory can be allocated during runtime.

malloc()

ptr = (int *)malloc(5 * sizeof(int));

Allocates memory for 5 integers.

1.8.20 Dynamic Array Example

Example Program

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int *arr;
    int i;
 
    arr = (int *)malloc(6 * sizeof(int));
 
    if(arr == NULL)
    {
        printf("Memory Allocation Failed");
        return 1;
    }
 
    for(i=0;i<6;i++)
    {
        arr[i] = (i+1)*10;
    }
 
    for(i=0;i<6;i++)
    {
        printf("%d ", arr[i]);
    }
 
    free(arr);
 
    return 0;
}

Output

10 20 30 40 50 60

1.8.21 Pointer to Pointer

A pointer can store the address of another pointer.

Syntax

int **pptr;

Example Program

#include <stdio.h>
 
int main()
{
    int x = 45;
 
    int *ptr = &x;
    int **pptr = &ptr;
 
    printf("%d\n", x);
    printf("%d\n", *ptr);
    printf("%d\n", **pptr);
 
    return 0;
}

Output

45
45
45

1.8.22 Void Pointer

A void pointer can store addresses of any data type.

Example

int num = 150;
 
void *ptr = &num;

Before using:

printf("%d", *(int *)ptr);

Output

150

1.8.23 Function Returning Pointer

Example Program

#include <stdio.h>
 
int *findMax(int *a,int *b)
{
    if(*a > *b)
        return a;
    else
        return b;
}
 
int main()
{
    int x = 90;
    int y = 120;
 
    int *result;
 
    result = findMax(&x,&y);
 
    printf("Largest = %d", *result);
 
    return 0;
}

Output

Largest = 120

1.8.24 Pointers and Strings

Strings are character arrays.

Example Program

#include <stdio.h>
 
int main()
{
    char *msg = "C Programming";
 
    printf("%s", msg);
 
    return 0;
}

Output

C Programming

1.8.25 Common Pointer Mistakes

Uninitialized Pointer

Wrong

int *ptr;
*ptr = 50;

Correct

int value = 50;
int *ptr = &value;

Forgetting free()

Wrong

ptr = malloc(100);

Memory leak occurs.

Correct

free(ptr);

Dereferencing NULL

Wrong

int *ptr = NULL;
 
printf("%d", *ptr);

This causes runtime errors.

1.8.26 Applications of Pointers

Pointers are widely used in:

Summary

Verify Comprehension: Technical Knowledge Assessment

Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.