-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc_grid.c
More file actions
54 lines (52 loc) · 808 Bytes
/
alloc_grid.c
File metadata and controls
54 lines (52 loc) · 808 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
52
53
54
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
/**
* alloc_grid - create a 2D array an initializes its elements to zero.
* @width: number of columns.
* @height: number of rows.
*
* Return: a double pointer to a 2D if successful, otherwise NULL.
*/
char **alloc_grid(int width, int height)
{
int i, j;
char **pp;
if (width == 0 || height == 0)
return (NULL);
pp = malloc(sizeof(*pp) * height);
if (pp == NULL)
{
free(pp);
return (NULL);
}
i = 0;
while (i < height)
{
pp[i] = malloc(sizeof(*pp[i]) * width);
if (pp[i] == NULL)
{
j = 0;
while (j <= i)
{
free(pp[j]);
j++;
}
free(pp);
return (NULL);
}
i++;
}
i = 0;
while (i < height)
{
j = 0;
if (j < width)
{
*(*(pp + i) + j) = '0';
j++;
}
i++;
}
return (pp);
}