0x0F. C Function pointers Task 2 - humtej1204/holbertonschool-low_level_programming GitHub Wiki
Write a function that searches for an integer.
- Prototype:
int int_index(int *array, int size, int (*cmp)(int));
- where
size
is the number of elements in the arrayarray
-
cmp
is a pointer to the function to be used to compare values -
int_index
returns the index of the first element for which thecmp
function does not return0
- If no element matches, return
-1
- If
size <= 0
, return-1
julien@ubuntu:~/0x0e. Function pointers$ cat 2-main.c
#include <stdio.h>
#include "function_pointers.h"
/**
* is_98 - check if a number is equal to 98
* @elem: the integer to check
*
* Return: 0 if false, something else otherwise.
*/
int is_98(int elem)
{
return (98 == elem);
}
/**
* is_strictly_positive - check if a number is greater than 0
* @elem: the integer to check
*
* Return: 0 if false, something else otherwise.
*/
int is_strictly_positive(int elem)
{
return (elem > 0);
}
/**
* abs_is_98 - check if the absolute value of a number is 98
* @elem: the integer to check
*
* Return: 0 if false, something else otherwise.
*/
int abs_is_98(int elem)
{
return (elem == 98 || -elem == 98);
}
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
int array[20] = {0, -98, 98, 402, 1024, 4096, -1024, -98, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 98};
int index;
index = int_index(array, 20, is_98);
printf("%d\n", index);
index = int_index(array, 20, abs_is_98);
printf("%d\n", index);
index = int_index(array, 20, is_strictly_positive);
printf("%d\n", index);
return (0);
}
julien@ubuntu:~/0x0e. Function pointers$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-int_index.c -o c
julien@ubuntu:~/0x0e. Function pointers$ ./c
2
1
2
julien@ubuntu:~/0x0e. Function pointers$
- GitHub repository: holbertonschool-low_level_programming
- Directory: 0x0F-function_pointers
- File: 2-int_index.c