Skip to content

Commit 0655c7a

Browse files
committed
Add 0x0F-function_pointers
1 parent ca7d3ef commit 0655c7a

File tree

16 files changed

+390
-0
lines changed

16 files changed

+390
-0
lines changed

0x0F-function_pointers/0-print_name.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "function_pointers.h"
2+
#include <stdlib.h>
3+
4+
/**
5+
* print_name - prints a name
6+
* @name: name to print
7+
* @f: pointer to the printing function
8+
*/
9+
void print_name(char *name, void (*f)(char *))
10+
{
11+
if (!name || !f)
12+
return;
13+
f(name);
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "function_pointers.h"
2+
#include <stdlib.h>
3+
4+
/**
5+
* array_iterator - executes a function given as a parameter
6+
* on each element of an array.
7+
* @array: array to iterate over
8+
* @size: size of the array
9+
* @action: pointer to function used
10+
*/
11+
void array_iterator(int *array, size_t size, void (*action)(int))
12+
{
13+
unsigned int i;
14+
15+
if (!array || !action)
16+
return;
17+
18+
for (i = 0; i < size; i++)
19+
action(array[i]);
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
/**
5+
* main - prints its own opcodes
6+
* @argc: number of arguments
7+
* @argv: array of arguments
8+
* Return: Always 0 (Success)
9+
*/
10+
int main(int argc, char *argv[])
11+
{
12+
int bytes;
13+
int index;
14+
int (*address)(int, char **) = main;
15+
unsigned char opcode;
16+
17+
if (argc != 2)
18+
{
19+
printf("Error\n");
20+
exit(1);
21+
}
22+
23+
bytes = atoi(argv[1]);
24+
25+
if (bytes < 0)
26+
{
27+
printf("Error\n");
28+
exit(2);
29+
}
30+
31+
for (index = 0; index < bytes; index++)
32+
{
33+
opcode = *(unsigned char *)address;
34+
printf("%.2x", opcode);
35+
36+
if (index == bytes - 1)
37+
continue;
38+
printf(" ");
39+
40+
address++;
41+
}
42+
43+
printf("\n");
44+
45+
return (0);
46+
}

0x0F-function_pointers/2-int_index.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "function_pointers.h"
2+
3+
/**
4+
* int_index - searches for an integer.
5+
* @array: array to search in
6+
* @size: size of the array
7+
* @cmp: pointer to the comparing function
8+
* Return: index of the first element for which the cmp
9+
* function does not return 0, or -1 if no match is found
10+
* or size is negative
11+
*/
12+
int int_index(int *array, int size, int (*cmp)(int))
13+
{
14+
int i;
15+
16+
if (array && cmp)
17+
{
18+
for (i = 0; i < size; i++)
19+
{
20+
if (cmp(array[i]) != 0)
21+
return (i);
22+
}
23+
}
24+
return (-1);
25+
}

0x0F-function_pointers/3-calc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef CALC_H
2+
#define CALC_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
/**
9+
* struct op - Struct op
10+
* @op: The operator
11+
* @f: The function associated
12+
*/
13+
typedef struct op
14+
{
15+
char *op;
16+
int (*f)(int a, int b);
17+
} op_t;
18+
19+
int op_add(int a, int b);
20+
int op_sub(int a, int b);
21+
int op_mul(int a, int b);
22+
int op_div(int a, int b);
23+
int op_mod(int a, int b);
24+
int (*get_op_func(char *s))(int, int);
25+
26+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "3-calc.h"
2+
3+
/**
4+
* get_op_func - function pointer that selects the correct
5+
* function to perform the operation asked by the user
6+
* @s: the operator given by the user
7+
* Return: pointer to the function that corresponds to the
8+
* operator given as a parameter
9+
*/
10+
int (*get_op_func(char *s))(int, int)
11+
{
12+
op_t ops[] = {
13+
{"+", op_add},
14+
{"-", op_sub},
15+
{"*", op_mul},
16+
{"/", op_div},
17+
{"%", op_mod},
18+
{NULL, NULL}
19+
};
20+
int i;
21+
22+
i = 0;
23+
24+
while (ops[i].op)
25+
{
26+
if (strcmp(ops[i].op, s) == 0)
27+
return (ops[i].f);
28+
i++;
29+
}
30+
31+
return (NULL);
32+
}

0x0F-function_pointers/3-main.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "3-calc.h"
2+
3+
/**
4+
* main - program that perfroms simple operations
5+
* @argc: number of arguments
6+
* @argv: array of arguments
7+
* Return: Always 0 (Success)
8+
*/
9+
int main(int argc, char *argv[])
10+
{
11+
int arg1, arg2, result;
12+
char o;
13+
int (*func)(int, int);
14+
15+
if (argc != 4)
16+
{
17+
printf("Error\n");
18+
exit(98);
19+
}
20+
21+
arg1 = atoi(argv[1]);
22+
arg2 = atoi(argv[3]);
23+
24+
func = get_op_func(argv[2]);
25+
26+
if (!func)
27+
{
28+
printf("Error\n");
29+
exit(99);
30+
}
31+
32+
o = *argv[2];
33+
34+
if ((o == '/' || o == '%') && arg2 == 0)
35+
{
36+
printf("Error\n");
37+
exit(100);
38+
}
39+
40+
result = func(arg1, arg2);
41+
42+
printf("%d\n", result);
43+
44+
return (0);
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "3-calc.h"
2+
3+
/**
4+
* op_add - calculates the sum of two integers
5+
* @a: first integer
6+
* @b: second integer
7+
* Return: sum of a and b
8+
*/
9+
int op_add(int a, int b)
10+
{
11+
return (a + b);
12+
}
13+
14+
/**
15+
* op_sub - calculates the difference of two integers
16+
* @a: first integer
17+
* @b: second integer
18+
* Return: difference of a and b
19+
*/
20+
int op_sub(int a, int b)
21+
{
22+
return (a - b);
23+
}
24+
25+
/**
26+
* op_mul - calculates the product of two integers
27+
* @a: first integer
28+
* @b: second integer
29+
* Return: product of a and b
30+
*/
31+
int op_mul(int a, int b)
32+
{
33+
return (a * b);
34+
}
35+
36+
/**
37+
* op_div - calculates the division of two integers
38+
* @a: first integer
39+
* @b: second integer
40+
* Return: result of the division of a and b
41+
*/
42+
int op_div(int a, int b)
43+
{
44+
return (a / b);
45+
}
46+
47+
/**
48+
* op_mod - calculates the remainder of the division of two integers
49+
* @a: first integer
50+
* @b: second integer
51+
* Return: remainder of a divided b
52+
*/
53+
int op_mod(int a, int b)
54+
{
55+
return (a % b);
56+
}

0x0F-function_pointers/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FILE 0-print_name.c is a function that prints a name.
2+
3+
FILE 1-array_iterator.c is a function that executes a function given as a parameter on each element of an array.
4+
5+
FILE 2-int_index.c is a function that searches for an integer.
6+
7+
FILES 3-main.c, 3-op_functions.c, 3-get_op_func.c, 3-calc.h are all part of a program that performs simple operations.

0x0F-function_pointers/a

16.5 KB
Binary file not shown.

0x0F-function_pointers/b

16.5 KB
Binary file not shown.

0x0F-function_pointers/c

16.5 KB
Binary file not shown.

0x0F-function_pointers/calc

16.8 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef FUNCTION_POINTERS_H
2+
#define FUNCTION_POINTERS_H
3+
4+
#include <stdlib.h>
5+
6+
void print_name(char *name, void (*f)(char *));
7+
void array_iterator(int *array, size_t size, void (*action)(int));
8+
int int_index(int *array, int size, int (*cmp)(int));
9+
10+
#endif

0x0F-function_pointers/main

16.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)