- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Memory Address
![]() Share with a Friend |
C Programming - C Memory Address
C Memory Address
In C programming, a memory address refers to the location where a variable or data is stored in the computer’s memory. Each variable in C is assigned a unique address, and this address can be accessed using pointers.
Memory addresses are an essential concept when working with pointers, as they allow you to directly interact with memory and manipulate data stored at specific locations.
Understanding Memory Address in C
- Address of Variables:
- Every variable in C is stored in a particular memory location, and that location has a unique address.
- The address of a variable can be obtained using the address-of operator &.
Syntax:
C
&variable_name
Example:
C
#include <stdio.h>
int main() {
int num = 5;
printf("Address of num: %p\n", &num);
return 0;
}
Output:
Address of num: 0x7ffeee7db12c // (This address will vary each time you run the program)
- Pointer and Memory Address:
- A pointer is a variable that holds the memory address of another variable. It stores the location (address) of the variable it points to.
- You can use pointers to indirectly access and modify the values stored at particular memory addresses.
Pointer Declaration:
C
type* pointer_name;
Example:
C
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // Pointer 'ptr' holds the address of 'num'
printf("Value of num: %d\n", num); // Output: 10
printf("Address of num: %p\n", &num); // Prints address of num
printf("Pointer ptr points to address: %p\n", ptr); // Same as &num
printf("Value at address pointed by ptr: %d\n", *ptr); // Output: 10
return 0;
}
Explanation:
- &num gives the memory address of the variable num.
- ptr is a pointer that stores the memory address of num.
- *ptr dereferences the pointer, accessing the value stored at the memory address ptr holds.
Memory Address and the Stack/Heap
- Stack Memory:
- When you declare local variables inside a function, their memory is allocated on the stack.
- The memory address of a local variable is typically temporary and only valid during the lifetime of the function.
- Heap Memory:
- Memory allocated dynamically (using functions like malloc(), calloc(), realloc(), and free()) is stored in the heap.
- The memory address of dynamically allocated variables remains valid until explicitly freed by the program.
Pointer Arithmetic and Memory Address Manipulation
In C, you can perform pointer arithmetic to move around memory addresses. This allows you to traverse through arrays or access memory locations using pointers.
Pointer Arithmetic Operations:
- Increment and Decrement:
- ptr++ increments the pointer to the next memory location based on the data type.
- ptr-- decrements the pointer to the previous memory location.
- Adding or Subtracting an Integer:
- You can add or subtract an integer to/from a pointer, which moves the pointer by that number of elements (not bytes) based on the data type.
Example:
C
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer points to the first element of the array
printf("Address of first element: %p\n", ptr);
printf("Value of first element: %d\n", *ptr);
// Pointer arithmetic
ptr++; // Move to the next element (next memory address)
printf("Address of second element: %p\n", ptr);
printf("Value of second element: %d\n", *ptr);
return 0;
}
Output:
Address of first element: 0x7ffeeed23b60
Value of first element: 10
Address of second element: 0x7ffeeed23b64 // Address increments by size of int (4 bytes)
Value of second element: 20
Memory Address Access via Arrays
Arrays in C are actually pointers to the first element. This means you can access the memory addresses of elements in an array using pointers.
- Array Indexing and Pointers:
- The expression arr[i] is equivalent to *(arr + i), meaning the pointer arr is moved by i positions.
Example:
C
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // Points to the first element of the array
for (int i = 0; i < 5; i++) {
printf("Address of arr[%d]: %p, Value: %d\n", i, ptr + i, *(ptr + i));
}
return 0;
}
Output:
Address of arr[0]: 0x7ffeeed23b60, Value: 1
Address of arr[1]: 0x7ffeeed23b64, Value: 2
Address of arr[2]: 0x7ffeeed23b68, Value: 3
Address of arr[3]: 0x7ffeeed23b6c, Value: 4
Address of arr[4]: 0x7ffeeed23b70, Value: 5
Conclusion
In C, memory addresses are critical for accessing and manipulating data in memory, particularly when working with pointers. By understanding how memory addresses work, you can write more efficient and flexible programs that interact with memory directly. Additionally, pointer arithmetic and memory management functions allow you to work with dynamic memory and optimize memory usage during program execution.
