2081 2

BIT102 ยท 2081 ยท 10 marks

What is pointer? How it differs from array?Write a program to add two 2D matrices.[4+6]

What is pointer? How it differs from array?Write a program to add two 2D matrices.[4+6]

C ProgrammingChapter 8: Pointers and Dynamic MemorySimilarity between arrays and pointers

Quick answer

A pointer is a variable that stores the memory address of another variable rather than storing a data value directly.

Pointer vs Array and 2D Matrix Addition


What is a Pointer? (4 marks)

Declaration syntax:

data_type *pointer_name;

Example:

int x = 10;
int *ptr;
ptr = &x;   // ptr holds the address of x
printf("%d", *ptr);  // prints 10 (dereferencing)
  • & operator gives the address of a variable
  • * operator dereferences a pointer (accesses value at that address)

How Pointer Differs from Array

FeaturePointerArray
DefinitionVariable that stores address of another variableCollection of elements of same data type stored in contiguous memory
MemoryStores only an address (typically 4 or 8 bytes)Allocates memory for all elements at once
AssignmentCan be reassigned to point to different variablesArray name is a constant pointer; cannot be reassigned
InitializationMust be explicitly assigned an address before useMemory is allocated at declaration
ArithmeticPointer arithmetic moves to next memory locationArray index directly accesses elements
sizeofReturns size of pointer variable itselfReturns total size of all elements
Dynamic memoryCan use malloc()/calloc() for dynamic allocationSize is fixed at compile time (static)

Example showing difference:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;

// arr = ptr;  // ERROR: array name is constant
ptr = ptr + 1;  // VALID: pointer can be moved

Program to Add Two 2D Matrices (6 marks)

#include <stdio.h>

int main() {
    int r, c, i, j;

    // Input dimensions
    printf("Enter number of rows: ");
    scanf("%d", &r);
    printf("Enter number of columns: ");
    scanf("%d", &c);

    int A[r][c], B[r][c], C[r][c];

    // Input matrix A
    printf("\nEnter elements of Matrix A (%dx%d):\n", r, c);
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("A[%d][%d]: ", i, j);
            scanf("%d", &A[i][j]);
        }
    }

    // Input matrix B
    printf("\nEnter elements of Matrix B (%dx%d):\n", r, c);
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("B[%d][%d]: ", i, j);
            scanf("%d", &B[i][j]);
        }
    }

    // Add matrices A and B, store result in C
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            C[i][j] = A[i][j] + B[i][j];
        }
    }

    // Display result matrix C
    printf("\nResultant Matrix C = A + B:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("%d\t", C[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Sample Output

Enter number of rows: 2
Enter number of columns: 2

Enter elements of Matrix A (2x2):
A[0][0]: 1
A[0][1]: 2
A[1][0]: 3
A[1][1]: 4

Enter elements of Matrix B (2x2):
B[0][0]: 5
B[0][1]: 6
B[1][0]: 7
B[1][1]: 8

Resultant Matrix C = A + B:
6    8
10   12

Logic Explanation

For two matrices A and B of same dimension (r x c):

$$C[i][j] = A[i][j] + B[i][j]$$

where i ranges from 0 to r-1 and j ranges from 0 to c-1.

Note: Matrix addition is only possible when both matrices have the same number of rows and columns.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.