← Back to list

Day 1: Introduction to Pointers in C

By the end of Day 1, you will understand what pointers are, why they are important, how they work in memory, and how to use them to access…

Tabspace · 2026-06-18 16:49 · 0 claps · 4.0 min read
#pointers #c-programming
Open on Medium ↗
Wiki topics: 💻 · Programming

Day 1: Introduction to Pointers in C

By the end of Day 1, you will understand what pointers are, why they are important, how they work in memory, and how to use them to access and modify variables efficiently. You will also learn how to print memory addresses, dereference pointers, and solve practical problems such as swapping two numbers using pointers.

1. What is a Pointer?

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

Normally, variables store values:

int num = 10;

Here:

  • Variable name = num
  • Value stored = 10

Every variable occupies a location in memory.

Example:

VariableValueMemory Addressnum101000

A pointer stores that address.

int *ptr = #

Now:

VariableStoresnum10ptr1000

The pointer points to the memory location of num.

2. Why Do We Need Pointers?

Pointers are one of the most powerful features of C.

They are used for:

Efficient Memory Access

Instead of copying large amounts of data, we can pass addresses.

Dynamic Memory Allocation

Functions like:

malloc()
calloc()
realloc()

use pointers.

Arrays and Strings

Arrays are heavily dependent on pointers.

Function Arguments

Pointers allow functions to modify original variables.

Data Structures

Linked Lists, Trees, Graphs, Hash Tables all rely on pointers.

Operating Systems

Memory management and device drivers use pointers extensively.

3. Understanding Computer Memory

Consider:

int age = 25;

The computer stores it somewhere in RAM.

Example:

Memory Address    Value
1000              25

The address may vary every time the program runs.

To access the address:

printf("%p", &age);

Output:

0x61ff08

The exact value will differ on each system.

4. Pointer Declaration

Syntax

data_type *pointer_name;

Examples:

int *ptr;
char *cptr;
float *fptr;
double *dptr;

Here:

  • ptr can store address of an integer
  • cptr can store address of a character
  • fptr can store address of a float

5. Pointer Initialization

A pointer should always be initialized before use.

Correct

int num = 10;
int *ptr = #

Incorrect

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

This may crash because the pointer contains garbage.

6. The Address-of Operator (&)

The & operator returns the address of a variable.

Example:

int x = 50;
printf("%p", &x);

Output:

0x61ff04

Diagram:

Variable x
Address: 1000
Value:   50

&x gives:

1000

7. The Dereference Operator (*)

The * operator accesses the value stored at an address.

Example:

int x = 50;
int *ptr = &x;
printf("%d", *ptr);

Output:

50

Diagram:

ptr ----> Address 1000

Address 1000 contains 50
*ptr = 50

8. Complete Example

#include <stdio.h>

int main()
{
    int num = 100;
    int *ptr = &num;
    printf("Value of num = %d\n", num);
    printf("Address of num = %p\n", &num);
    printf("Value stored in ptr = %p\n", ptr);
    printf("Value pointed by ptr = %d\n", *ptr);
    return 0;
}

Output:

Value of num = 100
Address of num = 0x61ff04
Value stored in ptr = 0x61ff04
Value pointed by ptr = 100

9. Pointer Types

Pointers must match the type of variable they point to.

Integer Pointer

int x = 10;
int *ptr = &x;

Character Pointer

char ch = 'A';
char *ptr = &ch;

Float Pointer

float f = 3.14;
float *ptr = &f;

Double Pointer

double d = 9.99;
double *ptr = &d;

10. Size of Pointers

Pointer size depends on system architecture.

Example:

printf("%zu", sizeof(int *));
printf("%zu", sizeof(char *));
printf("%zu", sizeof(float *));

Typical output on a 64-bit system:

8
8
8

Important:

The size of the pointer is usually the same regardless of data type.

11. Memory Representation of Pointers

Consider:

int x = 25;
int *ptr = &x;

Suppose:

Address  Value
1000     25
2000     1000

Memory view:

x
Address: 1000
Value:   25

ptr
Address: 2000
Value:   1000

Visualization:

ptr
 |
 v
+-------+
| 1000  |
+-------+
     |
     v
+-------+
|  25   |
+-------+
   x

12. Modifying Variables Through Pointers

Pointers can directly change the original variable.

Example:

int x = 10;
int *ptr = &x;
*ptr = 50;

Now:

printf("%d", x);

Output:

50

Because:

*ptr = 50

means:

Store 50 at address pointed by ptr

which is the address of x.

13. Multiple Pointers to the Same Variable

int num = 100;

int *ptr1 = &num;
int *ptr2 = &num;

Both pointers reference the same memory location.

*ptr1 = 500;

Now:

printf("%d", *ptr2);

Output:

500

14. Common Beginner Mistakes

Mistake 1: Using Uninitialized Pointers

int *ptr;
*ptr = 10;

Dangerous.

Mistake 2: Wrong Data Type

float f = 2.5;
int *ptr = &f;

Incorrect.

Mistake 3: Forgetting Dereference

int x = 10;
int *ptr = &x;

printf("%d", ptr);

This prints the address, not the value.

Use:

printf("%d", *ptr);

15. NULL Pointer

A pointer can intentionally point to nothing.

int *ptr = NULL;

Benefits:

  • Safer than garbage values
  • Easier debugging
  • Indicates “no valid address”

Example:

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

Hands-On Exercise: Print Addresses and Values

Program

#include <stdio.h>

int main()
{
    int num = 25;
    int *ptr = &num;
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    printf("Value stored in ptr: %p\n", ptr);
    printf("Value pointed by ptr: %d\n", *ptr);
    return 0;
}

Expected Learning

  • Using &
  • Using *
  • Understanding addresses
  • Understanding dereferencing

Assignment: Swap Two Numbers Using Pointers

Problem Statement

Write a program that swaps two numbers using pointers.

Input:

a = 10
b = 20

Output:

a = 20
b = 10

Solution

#include <stdio.h>

void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
int main()
{
    int a = 10;
    int b = 20;
    printf("Before Swap:\n");
    printf("a = %d, b = %d\n", a, b);
    swap(&a, &b);
    printf("After Swap:\n");
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Output

Before Swap:
a = 10, b = 20

After Swap:
a = 20, b = 10

Day 1 Summary

Today you learned:

What pointers are and why they are important

Memory addresses and memory representation

Address-of (&) operator

Dereference (*) operator

Pointer declaration and initialization

Different pointer types

How pointers access and modify variables

NULL pointers

Printing addresses and values

Swapping variables using pointers

Pointers form the foundation for advanced topics such as arrays, dynamic memory allocation, strings, structures, function pointers, and data structures. A strong understanding of today’s concepts will make the remaining days of the course much easier. On Day 2, you will explore Pointer Arithmetic and learn how pointers move through memory and arrays efficiently.


메타데이터
post_id
da1cba0b5014
slug
day-1-introduction-to-pointers-in-c-da1cba0b5014
url
https://medium.com/@tabspacepresentations/day-1-introduction-to-pointers-in-c-da1cba0b5014
canonical_url
https://medium.com/@tabspacepresentations/day-1-introduction-to-pointers-in-c-da1cba0b5014
author_url
https://medium.com/@tabspacepresentations
status
ok
fetched_at
2026-07-14 20:51:12