Skip to content
Snippets Groups Projects
Commit adb03400 authored by David Hendriks's avatar David Hendriks
Browse files

added some functions that use pointers to do stuff

parent 79b8d709
No related branches found
No related tags found
No related merge requests found
*.o
......@@ -8,6 +8,12 @@ int add_number(int number_1, int number_2) {
return result;
}
int multiply_number(int number_1, int number_2) {
int result = number_1 * number_2;
return result;
}
// https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c
char* make_str(const char *s1, const char *s2) {
......@@ -17,8 +23,10 @@ char* make_str(const char *s1, const char *s2) {
return result;
}
int add_number_using_pointer() {
int add_number_using_pointer(int (*pf)(int, int), int number_1, int number_2) {
int result;
result = pf(number_1, number_2);
return result;
}
......
......@@ -3,4 +3,6 @@
// char construct_string(int number);
int add_number(int number_1, int number_2);
char* make_str(const char *s1, const char *s2);
\ No newline at end of file
char* make_str(const char *s1, const char *s2);
int add_number_using_pointer(int (*pf)(int, int), int number_1, int number_2);
int multiply_number(int number_1, int number_2);
\ No newline at end of file
No preview for this file type
#include <stdio.h>
#include <stdlib.h>
#include "demolib.h"
int main(void) {
// Testing stuff for add_number function
int getal_1=2;
......@@ -9,7 +14,7 @@ int main(void) {
int result;
result = add_number(getal_1, getal_2);
printf("%d\n", result);
printf("result of adding numbers: %d\n", result);
//
......@@ -19,13 +24,41 @@ int main(void) {
char* s = make_str(str_1, str_2);
// do things with s
printf("%s\n", s);
printf("result of concatting strings: %s\n", s);
free(s); // deallocate the string
//
// testing stuff to
// testing stuff to use a pointer to use a function
int (*pf)(int, int);
int pointer_res;
pf = &add_number;
pointer_res = (pf)(5, 9);
printf("result of using a pointer to the add_number function: %d\n", pointer_res);
//
// testing stuff to pass a pointer to a function that consecutively uses that pointer to add stuff
int (*pf2)(int, int);
int function_via_pointer_res;
pf2 = &add_number;
function_via_pointer_res = add_number_using_pointer(pf2, 10, 20);
printf("result of calling a function with a function pointer to add_number: %d\n", function_via_pointer_res);
int (*pf3)(int, int);
int function_via_pointer_res_2;
pf3 = &multiply_number;
function_via_pointer_res_2 = add_number_using_pointer(pf3, 10, 20);
printf("result of calling a function with a function pointer to multiply_number: %d\n", function_via_pointer_res_2);
//
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment