Pointers and arrays Pointers Variables that hold memory addresses allowing for direct memory manipulation Arrays Collections of elements of the same type stored in contiguous memory Interaction Pointers can be used to access and manipulate array elements efficiently Array Declaration An array is a collection of elements of the same type stored in contiguous memory locations int arr 5 1 2 3 4 5 An array of integers Pointer Declaration A pointer can be declared to point to the type of the array elements int ptr Pointer to an integer Relationship Between Pointers and Arrays Array Name as Pointer The name of an array acts like a pointer to its first element For example int ptr arr ptr now points to the first element of arr Accessing Elements You can access array elements using both array notation and pointer arithmetic printf d n arr 0 Output 1 printf d n ptr 0 Output 1 same as arr 0 printf d n ptr 1 Output 2 same as arr 1 Iterating Through an Array include stdio h int main int arr 5 1 2 3 4 5 Array declaration int ptr arr Pointer to the first element of the array Accessing elements using pointer arithmetic for int i 0 i 5 i printf Element d d n i ptr i return 0
View Full Document