Pointers in c language: quick revision, 13 bites

All 13 revision bites from this kit, free and with no sign-up.

What is a Pointer?

A variable that stores a memory address.

A **pointer** is a variable that holds the **memory address** of another variable. Instead of storing a value directly, it stores the location where the value is kept. This allows indirect access to data, enabling dynamic memory management and efficient array handling.

Takeaway. Pointers store addresses, not values, enabling indirect data access.

Pointer Declaration and Initialization

Declare with * and initialize with &.

A pointer is declared by specifying the type of data it points to, followed by an asterisk. It is initialized with the address of a variable using the `&` operator.

Takeaway. Use `type *ptr;` to declare and `&variable` to initialize.

Dereferencing a Pointer

Access the value at the address.

The **dereference operator** `*` is used to access the value stored at the address held by a pointer. This allows reading or modifying the original variable indirectly.

Takeaway. `*ptr` gives the value at the address, enabling read and write.

Pointer Size and Type

Size is same, type matters.

On a given system, all pointers typically have the same size (e.g., 8 bytes on 64-bit), but the **type** determines how many bytes are accessed when dereferencing. An `int*` reads 4 bytes, while a `char*` reads 1 byte.

Takeaway. Pointer size is uniform, but type defines dereference granularity.

Pointers and Arrays

Array name is a constant pointer.

In C, the name of an array acts like a **constant pointer** to its first element. This means array elements can be accessed using pointer arithmetic. For example, `arr[i]` is equivalent to `*(arr + i)`.

Takeaway. Array indexing is syntactic sugar for pointer arithmetic.

Uninitialized Pointer

Dereferencing garbage causes crashes.

Takeaway. Always initialize pointers to avoid crashes.

Pointer to Pointer

Chain of addresses.

A **pointer to a pointer** stores the address of another pointer. It is useful for modifying pointer arguments in functions or creating dynamic 2D arrays.

Takeaway. `**pp` dereferences twice to reach the final value.

Passing Pointers to Functions

Modify variables outside the function.

Passing a pointer to a function allows the function to modify the original variable, because it receives the address. This is called **pass by reference** in C.

Takeaway. Pointer parameters enable functions to change caller's data.

Pointer Arithmetic

Adding 1 moves by type size.

Pointer arithmetic adjusts the address by the size of the pointed-to type. For an `int*`, `ptr + 1` moves 4 bytes forward (assuming 4-byte int).

Takeaway. Pointer increments scale by the size of the data type.

Pointers vs. Arrays

Similar but not identical.

Takeaway. Arrays are fixed-size blocks; pointers are flexible references.

Dynamic Memory Allocation

Pointers enable runtime memory.

Functions like `malloc()` and `free()` use pointers to manage **heap memory** at runtime. This allows creating data structures like linked lists and dynamic arrays whose size is not known at compile time.

Takeaway. Pointers are essential for dynamic memory management.

NULL Pointer Check

Always check for NULL.

Before dereferencing a pointer, especially one returned by `malloc` or passed to a function, check if it is `NULL`. This prevents crashes and is a common exam question.

Takeaway. Always validate pointers before use.

Remember & and *

& is address, * is value.

Think of `&` as 'address of' and `*` as 'value at'. In declarations, `*` means 'pointer to'. This simple mnemonic helps avoid confusion.

Takeaway. & gives address, * gives value.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages