C Programs Tutorials | IT Developer
IT Developer

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

  1. 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)

  1. 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 = &num;  // 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

  1. 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.
  2. 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:

  1. 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.
  2. 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.

  1. 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.