Briefly discuss the relation between pointers and arrays, giving suitable example. Write a program in C, to print transpose of a 2D matrix entered by a user. Also give comments.
Pointers and arrays have a close relationship in the C programming language. In fact, arrays can be thought of as a kind of pointer.
An array is a contiguous block of memory that stores multiple elements of the same data type. The elements in an array are accessed using an index that represents their position within the array. When an array is declared, the name of the array acts as a constant pointer to the first element of the array.
Pointers, on the other hand, are variables that store memory addresses. They can point to the address of another variable or object. In the case of arrays, a pointer can be used to access elements of the array.
To illustrate the relationship between pointers and arrays, let's consider an example:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Assigning the address of the first element to the pointer
printf("Array elements using array syntax:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\nArray elements using pointer syntax:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i)); // Dereferencing the pointer to access array elements
}
return 0;
}
In this example, we have an integer array arr with 5 elements. We also declare a pointer ptr and assign the address of the first element of the array to it. When we use the array syntax arr[i], we are directly accessing the elements of the array. Similarly, when we use the pointer syntax *(ptr + i),
_______ _____ _______ ___ ________ _________ ___ ____ _________ ____ ___.
_____ ______ __________ _____ ___ __________.
________ ______ ________ ______ ______ __________ ________ _____.
______ ________ ________ ___ ___ __________ ________ _____ __________ ______ __________.
_______ ________ _________ _________ _______.
_____ _____ __________ ______ _______ ______ ________ _____ _______ ____ _______ _________.
_______ _______ ____ _______ _______ ___ ______ ____ _______ ______ ____.
__________ _________ _____ ______ _______ ___ _________ _____.
___ ____ __________ __________ _______ ____ __________ _________ __________.
________ ______ ___ ____ _____ ________ _____ _______ _____ ____.
____ ______ _____ __________ ____ _______ _____ _________ _____ __________.
______ _____ ____ _____ _________ ___ _______ _______ ___ __________.
______ ______ ________ ____ ________.
__________ _____ _____ _______ ________ ___ ________.
________ _______ ________ ____ _______ _________ __________ ____ ________ __________.
____ _______ _______ _____ ___ __________ _____ ____.
________ ______ ____ ________ ______ _________ ______ ____ _______ ___.
___ _____ ___ ________ ___ ___ _______ ____ _______ _______ __________ _____.
________ _________ _____ ________ __________ ________ __________ ______ _________.
________ ______ ________ ______ ________ _______ _________ ______.
________ ____ ________ _____ ______ ______.
_____ ___ _________ _________ _____ ________ ________ __________ _________.
_________ _________ _____ _____ ___ ________ ___ ________ _____.
____ ________ ___ ______ __________ _______ _____.
______ ______ ____ _______ _______ ______ _______.
_____ _______ _______ ___ ____ _____ _________ ______.
_______ ____ ___ _________ __________ ________ _________ ____.
________ ______ __________ ____ ____ __________ ____ ______ _________.
___ _________ ____ _________ ________ _____ _______.
_____ __________ ________ ____ __________ ________ ______ _________ ________.
___ _____ ________ ____ ________ _________ _______.
_______ ____ ____ _____ ____ _____ _______ _______ _____ _______ _________.
__________ _______ _______ ______ ______ ___ _______ ____ ________ ___.
__________ ___ ____ _________ ___.
__________ ______ ________ _________ ____ ____ ___ ________ _________.
______ ____ ____ __________ _______ _____ _________ _____ ____ _____ ____ ____.
____ ________ _________ ____ ________ ________ __________ ______ _____ ______ _________ ____.
_____ ___ ________ _______ ____ __________ ___.
______ ___ __________ _____ _______.
___ _________ _______ ________ ____ ___ ___ _____.
__________ ________ _______ ________ _________ ____ _________.
____ ________ _______ ___ ________.
_____ __________ _____ _________ ________ _____ ___ ___.
_______ _____ ______ __________ _______ _____ ________.
____ _______ ________ _______ ____.
_______ _______ ____ ______ ______ ______ _____ ________.
_______ ______ __________ ____ ________ ______ __________ _______ ______ _______ ___ ________.
__________ ___ ______ ____ _____ __________ ______ _______ __________.
_______ __________ _______ ________ __________ __________ _____ ________ _____ ________.
_________ _______ _______ ______ __________.
________ ______ _____ ___ ___ _________ ___ _______ ______ ______ __________.
_________ __________ ______ ___ _________ _____ _______ _____ ________ __________.
_____ _____ _______ __________ _______ ____ _____.
_______ _______ ________ ____ __________ ________ ___ ______.
_______ _______ ____ _______ ________ ___.
________ _______ _____ ________ ________ _______ ___ ____.
_________ ________ ___ ______ ___ _________ ________ _______.
___ ___ ___ ___ ______ _____ _____ ___ __________ ____.
________ ___ __________ _______ ____ ______ __________ _______ _______.
_____ ___ __________ ______ ___ ______.
___ ________ __________ _____ _______ _______ ________ ________ _____ ___ ____.
______ _________ _________ ____ _______ ______ _____ __________ _______ ____.
_______ _______ __________ _______ _____ _______.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required