Advertisement
Guest User

C Code

a guest
May 26th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2. compile:
  3. gcc 2d_array_test_new.c -o 2d_array_test_new.out -std=c99 -O0 -Wall
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include <stdlib.h>
  9.  
  10. int create_matrix(size_t, size_t, int**);
  11.  
  12. int main(void)
  13. {
  14. int height = 10;
  15. int width = 10;
  16.  
  17. int (*matrix)[height][width] = NULL;
  18. if (-1 == create_matrix(height, width, &matrix))
  19. {
  20. perror("create_matrix() failed");
  21. }
  22. else
  23. {
  24. for (size_t row = 0; row < height; row++)
  25. {
  26. for (size_t col = 0; col < width; col++)
  27. {
  28. printf("%d, " , (*matrix)[row][col]);
  29. }
  30. printf("\n");
  31. }
  32. }
  33.  
  34. return EXIT_SUCCESS;
  35. }
  36.  
  37. int create_matrix(size_t rows, size_t cols, int(**a)[rows][cols])
  38. {
  39. int result = 0;
  40. int errno;
  41.  
  42. if (NULL == a)
  43. {
  44. result = -1;
  45. errno = EINVAL;
  46. }
  47. else
  48. {
  49. (*a) = malloc(sizeof **a);
  50. if (NULL == (*a))
  51. {
  52. result = -1;
  53. }
  54. else
  55. {
  56. for (size_t row = 0; row < rows; row++)
  57. {
  58. for (size_t col = 0; col < cols; col++)
  59. {
  60. (**a)[row][col] = row * cols + col;
  61. }
  62. }
  63.  
  64. for (size_t row = 0; row < rows; row++)
  65. {
  66. for (size_t col = 0; col < cols; col++)
  67. {
  68. printf("%d, " , (**a)[row][col]);
  69. }
  70. printf("\n");
  71. }
  72. }
  73. }
  74.  
  75. return result;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement