-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_realloc.c
63 lines (56 loc) · 1.01 KB
/
_realloc.c
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
55
56
57
58
59
60
61
62
63
#include "holberton.h"
/**
*_realloccharss - reallocates memory for char**
*@ptr: a pointer to an array
*@n: number of elements to add/subtract
*
*Return: pointer to new memory
*/
char **_realloccharss(char **ptr, int n)
{
int i = 0;
int newsize;
char **newptr;
if (ptr == NULL || *ptr == NULL)
return (NULL);
while (*(ptr + i) != NULL)
i++;
newsize = i + n;
newptr = malloc(sizeof(char *) * newsize + 1);
if (newptr == NULL)
return (NULL);
i = 0;
while (*(ptr + i) != NULL)
{
*(newptr + i) = *(ptr + i);
i++;
}
*(newptr + i) = NULL;
return (newptr);
}
/**
*_reallocchar - reallocates memory for char
*@ptr: a pointer to an array
*
*Return: pointer to new memory
*/
char *_reallocchar(char *ptr)
{
int i = 0;
char *newptr;
if (ptr == NULL)
return (NULL);
while (*(ptr + i) != '\n')
i++;
newptr = malloc(sizeof(char) * i + 1);
if (newptr == NULL)
return (NULL);
i = 0;
while (*(ptr + i) != '\n')
{
*(newptr + i) = *(ptr + i);
i++;
}
*(newptr + i) = '\0';
return (newptr);
}