Skip to content

Instantly share code, notes, and snippets.

@iordic
Last active February 10, 2018 23:00
Show Gist options
  • Save iordic/f89b7dfcbe0bc0d045214f14aaf97915 to your computer and use it in GitHub Desktop.
Save iordic/f89b7dfcbe0bc0d045214f14aaf97915 to your computer and use it in GitHub Desktop.
Dinamic matrix with memory allocation.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main() {
// Declare constant sizes
const int rows = 8;
const int cols = 6;
// Seed for random numbers generation
srand(time(NULL));
// Pointer for the matrix
int** ptr = (int**) malloc(sizeof(int*) * rows);
for (int i = 0; i < rows; i++) {
// Initialise pointer for every row
ptr[i] = (int*) malloc(sizeof(int) * cols);
for (int j = 0; j < cols; j++) {
ptr[i][j] = rand() % 10;
}
}
// Printing all the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", ptr[i][j]);
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment