Docsity

Pointers in C language, Cheat Sheet of Computer Programming

Toggle sidebar
bg1
Pointers in C programming are one of the most fundamental and powerful features of the
language. Simply put, a pointer is a variable that stores the memory address of another
variable. Think of it like a piece of paper with a house's street address written on it; the paper
itself isn't the house, but it tells you exactly where the house is located.
๐Ÿ’ก
Core Concepts of Pointers
1. Storing an Address
Every variable in your program is stored in a specific location in the computer's memory, which
has a unique address (like a house number). A pointer variable is designed to hold this memory
address.
2. Declaration
You declare a pointer variable using the asterisk (*) operator. The data type preceding the
asterisk specifies the type of data that the memory location pointed to holds.
Syntax:
data_type *pointer_name;
Example:
int *ptr; // ptr is a pointer that can hold the address of an
integer variable.
char *cPtr; // cPtr is a pointer that can hold the address of a
character variable.
3. The Address-of Operator (&)
The ampersand (&), often called the address-of operator, is used to get the memory address
of an existing variable.
Example:
int value = 100;
int *ptr;
ptr = &value; // ptr now stores the memory address where 'value' (100)
is stored.
4. The Dereference Operator (*)
The asterisk (*) used during accessing (not declaration) is called the dereference operator or
indirection operator. It means "go to the address stored in this pointer and get the value
located there."
Example (Continuing from above):
printf("Value of value: %d\n", value); // Output: 100
printf("Address of value: %p\n", &value); // Output: Some memory
pf2

Partial preview of the text

Download Pointers in C language and more Cheat Sheet Computer Programming in PDF only on Docsity!

Pointers in C programming are one of the most fundamental and powerful features of the language. Simply put, a pointer is a variable that stores the memory address of another variable. Think of it like a piece of paper with a house's street address written on it; the paper itself isn't the house, but it tells you exactly where the house is located.

๐Ÿ’ก Core Concepts of Pointers

1. Storing an Address

Every variable in your program is stored in a specific location in the computer's memory, which has a unique address (like a house number). A pointer variable is designed to hold this memory address.

2. Declaration

You declare a pointer variable using the asterisk ()* operator. The data type preceding the asterisk specifies the type of data that the memory location pointed to holds. Syntax: data_type *pointer_name;

Example: int *ptr; // ptr is a pointer that can hold the address of an integer variable. char *cPtr; // cPtr is a pointer that can hold the address of a character variable.

3. The Address-of Operator (&)

The ampersand (&), often called the address-of operator , is used to get the memory address of an existing variable. Example: int value = 100; int *ptr;

ptr = &value; // ptr now stores the memory address where 'value' (100) is stored.

4. The Dereference Operator (*)

The asterisk (*) used during accessing (not declaration) is called the dereference operator or indirection operator. It means "go to the address stored in this pointer and get the value located there." Example (Continuing from above): printf("Value of value: %d\n", value); // Output: 100 printf("Address of value: %p\n", &value); // Output: Some memory

address (e.g., 0x7ffe...) printf("Value in ptr: %p\n", ptr); // Output: Same memory address as above printf("Value pointed to by ptr: %d\n", *ptr); // Output: 100 (It looks at the address in ptr)

*ptr = 200; // This changes the value at the address stored in ptr (i.e., it changes 'value' to 200) printf("New value of value: %d\n", value); // Output: 200

โš™ Why Use Pointers?

Pointers are essential for several reasons: โ— Dynamic Memory Allocation: Pointers are crucial for allocating memory during runtime using functions like malloc() and calloc(). โ— Arrays and Strings: In C, the name of an array often acts as a constant pointer to its first element. Pointers allow for efficient traversal and manipulation of array elements. โ— Passing Arguments to Functions: Pointers allow functions to modify the original variables passed to them (known as pass by reference ), which is not possible with normal variables ( pass by value ). โ— Data Structures: Complex data structures like linked lists, trees, and graphs are built almost entirely using pointers to connect nodes. โ— Efficiency: Pointer arithmetic can sometimes lead to faster code execution compared to array indexing.

โžก Pointer Arithmetic

You can perform simple arithmetic operations on pointers, mainly incrementing (++) and decrementing (--). When you increment a pointer, it doesn't just move to the next byte in memory. Instead, it moves forward by the size of the data type it points to. If ptr is an int * (where an int is typically 4 bytes): โ— ptr++ moves the pointer forward by 4 bytes to point to the next integer. If cPtr is a char * (where a char is typically 1 byte): โ— cPtr++ moves the pointer forward by 1 byte to point to the next character. This feature is heavily used when iterating over arrays.

โš  Important Note on void* Pointers

A void* (void pointer) is a generic pointer that can hold the address of any data type. However, you cannot directly dereference a void* because the compiler doesn't know the size of the data it points to. It must first be cast to a specific pointer type (like int* or char*) before it can be dereferenced. Would you like me to explain how to use pointers to modify an array's contents inside a function?