C Access Memory

1. Accessing Memory Using Pointers

  • A pointer stores the address of a variable.

  • Use dereferencing (*) to access or modify the value at that memory address.

Syntax:



 


2. Example: Pointer Access


 

Output:

Value of a: 50
Modified a: 100

*ptr accesses the memory location of a.


3. Accessing Dynamically Allocated Memory

  • After allocating memory with malloc or calloc, use pointer arithmetic or dereferencing to access elements.


 

Output:

1 2 3 4 5

Array notation arr[i] is equivalent to *(arr + i).


4. Accessing Memory in Structures via Pointers


 

Output:

ID: 101, Name: Alice

Use -> operator to access struct members via pointer.


5. Pointer Arithmetic for Memory Access

  • Pointers can be incremented or decremented to traverse memory:


 

Output:

10 20 30 40 50

6. Key Points About Accessing Memory in C

  1. Pointers store addresses of variables or allocated memory.

  2. Use *ptr to read/write the value at that memory location.

  3. Array elements can be accessed using array notation (arr[i]) or pointer arithmetic (*(arr + i)).

  4. For structures, use dot . for normal variables and arrow -> for pointers.

  5. Always ensure the pointer points to valid memory before accessing it.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *