C Pointer Examples 🎯

beginner
19 min

C Pointer Examples 🎯

Welcome to our comprehensive guide on C Pointers! This tutorial is designed to help both beginners and intermediate learners understand the intricacies of pointers in C programming. By the end of this lesson, you'll be comfortable using pointers in your own projects. 📝 Note: This lesson is meant to be read sequentially for optimal learning.

Table of Contents

  1. Introduction to Pointers
  2. Understanding the Pointer Variable
  3. Pointer Notations
  4. Accessing Memory Locations
  5. Pointers and Arrays
  6. Pointers and Structures
  7. Dynamic Memory Allocation
  8. Pointers and Functions
  9. Practical Examples
  10. Quiz

<a name="intro"></a>

1. Introduction to Pointers 💡

Pointers are variables that store the memory address of another variable. They allow us to manipulate the memory locations directly, making them an essential tool for managing memory efficiently.

<a name="ptr_var"></a>

2. Understanding the Pointer Variable 📝

A pointer variable holds the memory address of another variable. To declare a pointer, we use the * symbol before the variable name.

c
int num = 10; int *ptr; // Declare a pointer to an integer ptr = &num; // Assign the address of num to ptr

<a name="ptr_not"></a>

3. Pointer Notations 💡

There are two notations for pointers:

  • Dereference notation: *ptr - Accesses the value stored at the memory address pointed by ptr
  • Address-of notation: &num - Returns the memory address of num

<a name="mem_loc"></a>

4. Accessing Memory Locations 💡

Accessing memory locations directly using pointers allows for flexibility and efficiency. Here's how to access and modify the value stored at a memory address:

c
int num = 10; int *ptr = &num; *ptr = 20; // Change the value stored at the address pointed by ptr printf("%d", num); // Output: 20

<a name="arrays"></a>

5. Pointers and Arrays 📝

Arrays can be treated as a sequence of memory locations, making pointers an ideal choice for array manipulation. To declare a pointer to an array, we use the [] symbol:

c
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; // Declare a pointer to an array printf("%d", *ptr); // Output: 1

<a name="struct"></a>

6. Pointers and Structures 📝

Pointers can also be used with structures, allowing us to manipulate complex data types more efficiently.

c
typedef struct { int id; char name[20]; } Person; Person person = {1, "John Doe"}; Person *ptr = &person; printf("%d", ptr->id); // Output: 1

<a name="mem_alloc"></a>

7. Dynamic Memory Allocation 💡

Dynamic memory allocation allows us to create variables at runtime, making our programs more flexible. The malloc() function is used to dynamically allocate memory:

c
int *ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers ptr[0] = 10; ptr[1] = 20; ...

<a name="funcs"></a>

8. Pointers and Functions 💡

Pointers can be used to pass and return data to functions, allowing for modular and reusable code.

c
void increment(int *ptr) { (*ptr)++; } int num = 10; int *ptr = &num; increment(ptr); printf("%d", num); // Output: 11

<a name="examples"></a>

9. Practical Examples 💡

For a deeper understanding, check out these practical examples using pointers in C programming:

<a name="quiz"></a>

10. Quiz 💡

Test your understanding with these quiz questions:

Quick Quiz
Question 1 of 1

What is the output of the following code?

Quick Quiz
Question 1 of 1

What does the `[]` symbol represent in a pointer declaration?