Skip to content

Commit 75740b5

Browse files
committed
improve: change behaviour of ft_ca2ia and add comment
1 parent bee3d9e commit 75740b5

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

sources/conversions/ft_ca2ia.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,34 @@
66
/* By: amalsago <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/02/21 23:53:51 by amalsago #+# #+# */
9-
/* Updated: 2020/02/22 01:22:04 by amalsago ### ########.fr */
9+
/* Updated: 2020/02/23 11:41:57 by amalsago ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

15+
/*
16+
** ft_ca2ia() allocates enough memory to store integers extracted from a given
17+
** array of chars.
18+
** The first element of `char_array` will be at the last position of `int_array`
19+
**
20+
** Example:
21+
** Given char_array = ["1", "2", "3", "4", "5"]
22+
** Calling ft_ca2ia(char_array) return int_array = [5, 4, 3, 2, 1]
23+
*/
24+
1525
int *ft_ca2ia(char **char_array)
1626
{
1727
int i;
1828
int array_size;
1929
int *int_array;
2030

21-
i = -1;
31+
i = 0;
2232
array_size = ft_arraysize(char_array);
2333
if (!(int_array = (int *)ft_memalloc(sizeof(int) * array_size)))
2434
return (NULL);
2535
--array_size;
2636
while (array_size >= 0)
27-
{
28-
++i;
29-
--array_size;
30-
int_array[i] = ft_atoi(char_array[i]);
31-
}
37+
int_array[i++] = ft_atoi(char_array[array_size--]);
3238
return (int_array);
3339
}

0 commit comments

Comments
 (0)