Question

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.

05 Mar 2024
Answer :
Word Count : 1135

In programming languages like C and Python, pointers and arrays have a closely intertwined relationship. While Python handles arrays differently than C, it’s important to understand how the concept of pointers works in C and how it relates to arrays.

### Relation between Pointers and Arrays in C:

In C, arrays and pointers are closely related. An array is essentially a block of contiguous memory locations, and a pointer is a variable that stores the address of another variable, including the address of the first element of an array.

When we declare an array in C, for example:

```c
int arr[5] = {1, 2, 3, 4, 5};
```

Here, `arr` is not just an array but also acts like a pointer to the first element of the array. In fact, the name of the array `arr` is equivalent to a pointer to the first element of the array. This means that the expression `arr` is pointing to the first element of the array `arr[0]`.

In C, we can use pointers to access elements of the array. For example:

```c
int *ptr = arr;  // Pointer ptr now holds the address of arr[0]
printf("%d\n", *ptr);  // Prints the value at the address pointed to by ptr (arr[0])
```

We can also use pointer arithmetic to traverse through the array. The following code demonstrates this:

```c
for (int i = 0; i < 5; i++) {
    printf("%d ", *(arr + i));  // Equivalent to arr[i]
}
```

### Array Access through Pointers:

Pointers and arrays are also interconnected when it comes to passing arrays to functions. In C, when we pass an array to a function, we are actually passing a pointer to the first element of the array. This allows the function to manipulate the contents of the array directly.

For example, if we pass the array `arr` to a function:

```c
void printArray(int *arr, int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", *(arr + i));  // Accessing array elements via pointer
    }
}
```

We can call the function `printArray` as follows:

```c
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5);
```

### Pointers and Arrays in Python:

In Python,

____ ________ __________ ___ ____ __________ _______ ________ _____ __________ _______.
__________ __________ __________ ____ ______.
______ ______ ____ ____ ______ ________ _______ ____ __________ ___ ____.
________ _________ ________ _________ ____ _________.
____ ___ _________ ______ ____ _____.
________ _____ _______ __________ ________ _________ _________ ________ ________ ____ ______ ______.
________ _____ ________ ________ _______ _________ ________.
____ ____ ___ __________ _______.
___ ___ ___ ____ ________ ______ ______ ________ ____ ___ ____.
______ ______ _________ ______ ______ _______.
_______ _______ ______ __________ ____ _______ _____.
___ ______ _________ _______ _______ __________ ______ _____ ________ __________.
___ ______ ________ ________ __________ __________ _____ ____ __________ _________ _____.
_______ ______ _________ ___ ____ _________ _______ ____ _______.
___ _____ _____ _____ ________ ______ ___ ______ ____ ____ _________.
____ _________ _______ ________ _______.
______ __________ _________ ________ ____ _______ ________ _________ ____ ____ _________.
_____ ________ __________ _______ ________ ________ _______ ____.
___ ___ _____ _________ _________ ______ ___ _______ _________ _______ ______ _________.
____ _______ ______ _________ ______ __________ __________ _____ _________ _______ ____ ____.
________ __________ _____ _________ _____ __________ _______ __________ ________ ___ ______ ________.
________ ___ __________ ______ _______ ____ ___ _______ _____.
__________ _____ _______ _____ _______ _______ _______ ____ __________ _____ _______ __________.
____ ________ _______ __________ ___ ____ ______ _________ ____.
________ _______ ____ _______ _____ _______ ________ _______.
_____ ________ __________ _______ ___ ________ __________ _______ ________ _____.
_____ ________ _______ _____ __________ ___ ____.
________ _________ ________ ____ ________ ______ ______ ______ _____ _________ _________.
____ ____ ______ ______ _________.
________ ___ ____ _________ _____ ________ _________.
_________ _________ ___ ________ _____ ______.
_____ ______ ____ _________ _________ __________ __________.
__________ ________ _________ _______ ____ __________ ____ ____ ________.
_____ ______ ___ _________ ___ _________ _______ _______ _________ ___ ____ _________.
___ ________ _______ ____ _____ _________ __________ ____.
___ ___ ___ ______ __________ _________ _______ ____ ___.
________ __________ ____ _________ _________ ______ _______ ____ _____ ___ _______ ______.
___ _________ __________ __________ _______ _______ ______ _____ ________ ____.
___ ___ _______ _________ ____ _____ _____ ___ ________ ____ _________.
_________ _______ ___ _______ _____ ___ ____ _____ ______.
__________ _______ ___ __________ _____ ________ ______ ________ ___.
________ ___ _____ ____ _____ _____ ___ ______ ________ _____.
______ ____ ___ ______ ______ ______ _____ _________ ___ _____ ___ ______.
________ _________ _______ ________ ___ ______ ___.
_____ _____ ______ ___ ______ _____ ____ ________ ___ ______ _______ ___.
__________ _________ ________ __________ _____ ___ _________ _______ ______ ____ __________.
__________ ____ ________ ____ ___ ____ ___ _____ ________ ____ _______ ______.
___ _______ _________ ________ _____ ________ _______.
_______ ________ ______ _____ _________ ____ ______ ____.
_________ ___ ____ __________ ______ _________ ________ ___ _____ ________.
______ _______ ______ _________ _________ _________ ____ ____ _______.
_______ __________ ________ ____ _______ ____.
___ ________ ___ __________ _____ _____ _______ ________ ___ ______ ____.
________ _____ __________ ______ _______ _________ _____ __________ ____ _________ _________.
___ ___ _____ ___ _________ _____ _________ ___ ________ _______.
______ ____ __________ ___ ____ ____.
____ ______ ____ ____ _________ _____ ___ ______ ____ __________ _____.
________ _________ ________ _________ ___ ____ _________ _______ ___.
______ ______ ______ ___ ______ ________ _________ ____.
___ ______ __________ __________ _____.
______ ________ ______ ____ _______ _____ _______.
____ ___ ___ _____ __________ ___ _________ ____.
_____ ______ __________ __________ _______ ___ ______ ______ _________ ___ ______.
________ ______ __________ _____ _____ _____ _________ ____ ______ __________ _________ ___.
__________ _____ __________ _____ ____ ________ _________ ______ ____ ______ _____.
_______ _______ _________ ________ __________ _______ ____ _________ _________ _______ __________.
_____ __________ _____ _________ ________ _______ ____ _______ _____ _____ _________.
______ ____ ___ _____ ___ ______.
___ ____ _________ ___ ______ _______ _________ _____ __________.
_______ __________ ______ __________ ____ _____ _______.
___ _____ __________ _____ _________ _________ ___.
_______ ______ ____ __________ _________ ________ _________ ___ __________ _______ ______ ___.
______ _________ ____ ____ _______ __________ _____.
_________ __________ _______ ________ _____ _____ _____ ________ __________ ___ ____ _____.
________ _____ ____ _____ __________ ____ __________ ____ ___ _________.
__________ _____ ________ ____ ______ ________ ____ _________ ____.
_______ _____ ________ ______ _______ ______ __________ _____ _____ _________.
____ _______ _______ _______ ____ __________ ______ ___.
_____ ________ ___ ______ ____ _____ _______ ___ _________ ____.
________ _____ _______ _________ ______.
________ ____ ___ _______ ___ ________ _____ _______ ______ ______ __________ __________.
IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
Top
📞
Call Support Instant phone assistance IGNOU MCS 201 2024 - Briefly discuss the relation between pointers and arrays, giving suita | IGNOU Academy
→
🟢
WhatsApp Chat Fast live messaging
→
Email Us Business enquiries & support
→