Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- compile:
- gcc 2d_array_test_new.c -o 2d_array_test_new.out -std=c99 -O0 -Wall
- */
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- int create_matrix(size_t, size_t, int**);
- int main(void)
- {
- int height = 10;
- int width = 10;
- int (*matrix)[height][width] = NULL;
- if (-1 == create_matrix(height, width, &matrix))
- {
- perror("create_matrix() failed");
- }
- else
- {
- for (size_t row = 0; row < height; row++)
- {
- for (size_t col = 0; col < width; col++)
- {
- printf("%d, " , (*matrix)[row][col]);
- }
- printf("\n");
- }
- }
- return EXIT_SUCCESS;
- }
- int create_matrix(size_t rows, size_t cols, int(**a)[rows][cols])
- {
- int result = 0;
- int errno;
- if (NULL == a)
- {
- result = -1;
- errno = EINVAL;
- }
- else
- {
- (*a) = malloc(sizeof **a);
- if (NULL == (*a))
- {
- result = -1;
- }
- else
- {
- for (size_t row = 0; row < rows; row++)
- {
- for (size_t col = 0; col < cols; col++)
- {
- (**a)[row][col] = row * cols + col;
- }
- }
- for (size_t row = 0; row < rows; row++)
- {
- for (size_t col = 0; col < cols; col++)
- {
- printf("%d, " , (**a)[row][col]);
- }
- printf("\n");
- }
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement