Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(void)
- {
- int **a; // why?
- int rows = 3, columns = 4;
- /* Allocate memory for row pointers */
- if ((a = (int **)malloc(rows * sizeof(int *))) == NULL)
- exit(EXIT_FAILURE);
- /* Allocate memory for rows */
- for (int y = 0; y < rows; y++)
- {
- if ((a[y] = (int *)malloc(columns * sizeof(int))) == NULL)
- exit(EXIT_FAILURE);
- }
- /* Fill matrix with some data */
- for (int y = 0; y < rows; y++)
- {
- for (int x = 0; x < columns; x++)
- a[y][x] = 10 * (y + 1) + (x + 1);
- }
- /* Print matrix to the console */
- for (int y = 0; y < rows; y++)
- {
- for (int x = 0; x < columns; x++)
- printf("%2d ", a[y][x]);
- putchar('\n');
- }
- /* Free matrix memory */
- for (int y = 0; y < rows; y++)
- free(a[y]);
- free(a);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement