Docsity

Pointers and Memory allocation in C programming, Schemes and Mind Maps of C programming

Toggle sidebar
bg1
Master Guide: Understanding Pointers & Memory
Allocation in C
1. What is a Pointer?
A pointer is a variable that stores the memory address of another
variable, rather than storing a direct value (like an integer or character).
Think of a normal variable as a house, and a pointer as the mail carrier
holding the
street address
to that house.
int num = 45; // A normal integer variable
int *ptr; // Declaring a pointer that points to an integer
ptr = # // Storing the address of 'num' inside 'ptr'
2. Core Operators You Must Know
To master pointers, you only need to understand two critical symbols:
The Ampersand (&) | "Address-of" Operator: This retrieves the
exact memory address (location) where a variable is sitting.
The Asterisk (*) | "Dereferencing" Operator: When placed in
front of an existing pointer variable, it means
"go to the address
stored here and give me the actual value inside."
Memory Visualization
Imagine your computer's RAM looks like this grid:
Variable
Name
Memory
Address Value Stored
num 0x7ffee3 45
ptr 0x7ffee8 0x7ffee3
(Points to
num)
3. Pointer Syntax Breakdown (Cheat Sheet)
#include <stdio.h>
int main() {
pf3

Partial preview of the text

Download Pointers and Memory allocation in C programming and more Schemes and Mind Maps C programming in PDF only on Docsity!

Master Guide: Understanding Pointers & Memory

Allocation in C

1. What is a Pointer? A pointer is a variable that stores the memory address of another variable, rather than storing a direct value (like an integer or character). Think of a normal variable as a house, and a pointer as the mail carrier

holding the street address to that house.

int num = 45; // A normal integer variable

int *ptr; // Declaring a pointer that points to an integer

ptr = # // Storing the address of 'num' inside 'ptr'

2. Core Operators You Must Know To master pointers, you only need to understand two critical symbols:  The Ampersand (&) | "Address-of" Operator: This retrieves the exact memory address (location) where a variable is sitting.  The Asterisk () | "Dereferencing" Operator:* When placed in

front of an existing pointer variable, it means "go to the address

stored here and give me the actual value inside."

Memory Visualization Imagine your computer's RAM looks like this grid: Variable Name Memory Address Value Stored num 0x7ffee3 45 ptr 0x7ffee

0x7ffee3 (Points to

num)

3. Pointer Syntax Breakdown (Cheat Sheet) #include int main() {

int age = 20; int *p = &age; printf("Value of age: %d\n", age); // Outputs: 20 printf("Address of age: %p\n", &age); // Outputs something like: 0x7ffee // Dereferencing printf("Value stored at p: %d\n", *p); // Outputs: 20 // Changing the value via the pointer *p = 21; printf("New value of age: %d\n", age); // Outputs: 21 return 0; }

4. Dynamic Memory Allocation (DMA) By default, C allocates memory automatically (Static Allocation). However, if you don't know how much memory you'll need until the program runs, you must use Dynamic Memory Allocation from the library. The Big 3 DMA Functions 1. malloc() (Memory Allocation): Allocates a specific block of memory in bytes. It leaves the memory full of "garbage values" (old leftover data).

int arr = (int) malloc(5 * sizeof(int)); // Allocates space for 5 integers

  1. calloc() (Contiguous Allocation): Allocates memory and automatically initializes all bytes to zero. Great for clearing out data safely.

int arr = (int) calloc(5, sizeof(int)); // Allocates space and zeroes it out