How would you differentiate between the terms pointer to an array and array of pointers? Give proper examples.
The terms "pointer to an array" and "array of pointers" refer to different concepts in C programming, distinguished by how they are declared and used.
1. **Pointer to an Array**:
- A pointer to an array is a pointer variable that points to the first element of an array.
- When you have a pointer to an array, you can access elements of the array using pointer arithmetic.
- The pointer to an array points to the memory address where the array is stored.
- Example:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int (*ptr)[5]; // Pointer to an array of 5 integers
ptr = &arr; // Assigning the address of arr to ptr
printf("First element of array: %d\n", (*ptr)[0]); // Output: 1
____ _____ ____ _____ _____ _________ ______ _______ __________ ____ _________.
_______ __________ ______ _________ ________ ___ _____ ____ ______ _____.
_______ __________ _____ ___ _______ ___.
_______ ___ _____ ______ ____ __________ ___ __________ _________ ________ ______.
____ ___ _____ ______ ______ ____ _____ ___.
_______ ________ __________ ___ ____ __________ ____ ________ _______.
________ __________ ____ ________ ____ ___ ________ ___ __________ _______.
_____ ____ ___ ______ ___ __________.
________ _______ __________ ________ _____ _________ ________ ____ ____.
________ _______ ___ ______ ________ __________.
______ ________ ___ ________ ______ __________ ________ ______ _________ ___.
_________ ___ _______ ______ ____ ___ __________ ____ ____ ____ ___.
______ __________ __________ __________ __________ _____.
__________ _____ _________ ____ __________ ____ ________.
______ ___ ________ _______ _____ ___ _________ ____ _______ __________ ____.
__________ ____ ______ ____ __________ ______ _____.
_______ ____ ____ __________ ________ ______ _____.
_______ __________ ________ _______ _________ ___ ____ ______ __________ ________ _______.
_____ _________ __________ ________ ___ _________.
______ _____ ____ _____ _____ _____ _________ _____ _________ ________ ________.
______ __________ __________ ________ __________.
_____ _____ __________ ________ ______ ___.
___ ____ ________ _______ ____ _____ _______.
_____ _____ ____ _______ ______ ___.
__________ ___ _______ _____ ____ _____.
____ ________ _______ __________ ________ __________ _______ ______ ________ _______.
______ _____ __________ __________ ________ __________ ____ _________ _______ ________ _____.
___ _____ __________ __________ _________ ____ ____ _______ _______ ________ _______ _________.
________ ______ ____ ______ _________ ____ ______.
______ _______ ___ _________ _______ ________ _______ ______ ____ _________ _________ ________.
_______ ________ ___ ___.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required