C Online Compiler
Example: Matrix Addition Using Pointers in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Matrix Addition Using Pointers #include <stdio.h> #include <stdlib.h> // For malloc and free // Function to dynamically allocate a matrix int** allocateMatrix(int rows, int cols) { int** matrix = (int**)malloc(rows * sizeof(int*)); if (matrix == NULL) { perror("Memory allocation failed for rows"); exit(EXIT_FAILURE); } for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); if (matrix[i] == NULL) { perror("Memory allocation failed for columns"); // Free previously allocated rows before exiting for (int j = 0; j < i; j++) { free(matrix[j]); } free(matrix); exit(EXIT_FAILURE); } } return matrix; } // Function to read elements into a matrix void readMatrix(int** matrix, int rows, int cols) { printf("Enter elements:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("Enter element [%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } } } // Function to add two matrices using pointers void addMatrices(int** mat1, int** mat2, int** result, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { // Accessing elements using array-like indexing on pointers result[i][j] = mat1[i][j] + mat2[i][j]; // Alternative using pointer arithmetic: // *(*(result + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j); } } } // Function to print a matrix void printMatrix(int** matrix, int rows, int cols) { printf("Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d\t", matrix[i][j]); } printf("\n"); } } // Function to free dynamically allocated matrix memory void freeMatrix(int** matrix, int rows) { if (matrix == NULL) return; for (int i = 0; i < rows; i++) { free(matrix[i]); // Free each row } free(matrix); // Free the array of row pointers } int main() { int rows, cols; // Step 1: Get dimensions from the user printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); // Step 2: Dynamically allocate memory for the two input matrices and the result matrix int** matrixA = allocateMatrix(rows, cols); int** matrixB = allocateMatrix(rows, cols); int** resultMatrix = allocateMatrix(rows, cols); // Step 3: Read elements for Matrix A printf("\n--- For Matrix A ---\n"); readMatrix(matrixA, rows, cols); // Step 4: Read elements for Matrix B printf("\n--- For Matrix B ---\n"); readMatrix(matrixB, rows, cols); // Step 5: Add the two matrices addMatrices(matrixA, matrixB, resultMatrix, rows, cols); // Step 6: Print the matrices and their sum printf("\n--- Matrix A ---\n"); printMatrix(matrixA, rows, cols); printf("\n--- Matrix B ---\n"); printMatrix(matrixB, rows, cols); printf("\n--- Sum Matrix (A + B) ---\n"); printMatrix(resultMatrix, rows, cols); // Step 7: Free dynamically allocated memory freeMatrix(matrixA, rows); freeMatrix(matrixB, rows); freeMatrix(resultMatrix, rows); return 0; }
Output
Clear
ADVERTISEMENTS