
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- 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
- C - Predefined Identifier __func__
- 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
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- 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 - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Memory Address in C
Memory Address in C
The memory address is assigned to a variable when a variable is declared in C language. C compiler stores the value of the variable in the different segments of the memory.
Segments of Memory
Different elements of a C program are stored in different segments of the computers memory, which has the following segments −
- Text segment − A text segment, also known as a code segment or simply as text, is one of the sections of a progris used to store the object version of the C program.
- Initialized data segment − The global variables and static variables that are initialized by the programmer are allocated the memory in the initialized data segment.
- Uninitialized data segment − An uninitialized data segment also called as bss (stands for block started by symbol). The program allocates memory for this segment when it loads. Every data in bss is initialized to arithmetic "0" and pointers to null pointer by the kernel before the C program executes.
- Stack − Stack is a LIFO (last in first out) data structure. Stack segment stores the value of local variables and values of parameters passed to a function. It also maintains the pointer to which a function call returns.
- Heap − Heap is used for allocating memory during the runtime. All the functions that perform dynamic memory allocation deal with heap.
Accessing Memory Address
The memory addresses in C can be accessed or specified through the Address of (&) operator. To print a memory address using the printf() function, you need to use %p format specifier.
Syntax
Below is the syntax to access memory address −
&variable_name
Example
In the following example, we are declaring two variables and printing their memory addresses −
#include <stdio.h> int main() { // Declaring two variables int a; int b; // Accessing their memory // addresses and print them printf("Memory address of a is %p\n", &a); printf("Memory address of b is %p\n", &b); return 0; }
How Does C Compiler Allocate Memory?
Memory can be considered of as an array of bytes where each address is on index in the array and holds 1 byte.
When you declare a variable in a C program, the C compiler allocates a random memory location to it, depending on the size requirement, which depends on the type.
When an int variable is declared −
int x = 10;
The compiler assigns the value in a random byte address. Since an int type needs 4 bytes, the next four addresses are earmarked for it.
C allows you to find out which address has been allocated to a variable. You can use the %p format specifier to print the hexadecimal address of the memory location.
char x = 'A'; printf ("address of x: %p\n", &x);
This prints the address of "x" in hexadecimal format −
Address of x: 000000000061FE1F
Example
Arrays in C are contiguous memory areas that hold a number of values of the same data type (int, long, *char, etc.).
#include <stdio.h> int main() { // initialize an array of ints int numbers[5] = {1,2,3,4,5}; int i = 0; // print the address of the array variable printf("numbers = %p\n", numbers); // print addresses of each array index do { printf("numbers[%u] = %p\n", i, (void *)(&numbers[i])); i++; } while(i < 5); // print the size of the array printf("sizeof(numbers) = %lu\n", sizeof(numbers)); }
Output
When you run this code, it will produce the following output −
numbers = 0x7fff0815c0e0 numbers[0] = 0x7fff0815c0e0 numbers[1] = 0x7fff0815c0e4 numbers[2] = 0x7fff0815c0e8 numbers[3] = 0x7fff0815c0ec numbers[4] = 0x7fff0815c0f0 sizeof(numbers) = 20