Skip to content

Commit 99ab672

Browse files
committed
added: 1-array_iterator.c
1 parent d7b8b77 commit 99ab672

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "function_pointers.h"
2+
3+
/**
4+
* array_iterator - executes a function given as param on each elem of and arr.
5+
*
6+
* @array: array to be processed.
7+
* @size: size of the array.
8+
* @action: func to be called on each elem of the array
9+
*
10+
* Return: Nothing.
11+
*/
12+
void array_iterator(int *array, size_t size, void (*action)(int))
13+
{
14+
size_t index = 0;
15+
16+
while (index < size && array != NULL && action != NULL)
17+
{
18+
action(array[index]);
19+
index++;
20+
}
21+
}

0x0F-function_pointers/1-main.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include "function_pointers.h"
3+
4+
/**
5+
* print_elem - prints an integer
6+
* @elem: the integer to print
7+
*
8+
* Return: Nothing.
9+
*/
10+
void print_elem(int elem)
11+
{
12+
printf("%d\n", elem);
13+
}
14+
15+
/**
16+
* print_elem_hex - prints an integer, in hexadecimal
17+
* @elem: the integer to print
18+
*
19+
* Return: Nothing.
20+
*/
21+
void print_elem_hex(int elem)
22+
{
23+
printf("0x%x\n", elem);
24+
}
25+
26+
/**
27+
* main - check the code
28+
*
29+
* Return: Always 0.
30+
*/
31+
int main(void)
32+
{
33+
int array[5] = {0, 98, 402, 1024, 4096};
34+
35+
array_iterator(array, 5, &print_elem);
36+
array_iterator(array, 5, &print_elem_hex);
37+
return (0);
38+
}

0x0F-function_pointers/function_pointers.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
int _putchar(char);
66
void print_name(char *, void (*)(char *));
7+
void array_iterator(int *, size_t, void (*)(int));
78

89

910
#endif /* FUNCTION_POINTERS_H */

0 commit comments

Comments
 (0)