-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path3-alloc_grid.c
More file actions
51 lines (42 loc) · 746 Bytes
/
Copy path3-alloc_grid.c
File metadata and controls
51 lines (42 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "holberton.h"
#include <stdio.h>
#include <stdlib.h>
/**
* **alloc_grid - function to allocate memory to grid
*
* @width: int arg
*
* @height: int arg
*
* Return: grid of 0s
*/
int **alloc_grid(int width, int height)
{
int col, row;
int **doublePtr;
if (width < 1 || height < 1)
{
return (NULL);
}
doublePtr = malloc(height * sizeof(int *));
if (doublePtr == NULL)
{
return (NULL);
}
for (col = 0; col < height; col++)
{
doublePtr[col] = malloc(width * sizeof(int));
if (doublePtr[col] == NULL)
{
for (row = 0; row < col; row++)
free(doublePtr[row]);
free(doublePtr);
return (NULL);
}
for (row = 0; row < width; row++)
{
doublePtr[col][row] = 0;
}
}
return (doublePtr);
}