Function Pointer

Hi, I've been stuck on a problem for a couple hours I managed to code it to work without function pointers but when I go to use the function pointers it just prints my inputs where it is suppose to adjust the values so they are either ascending or descending depending on the character I input, I'm not sure what is wrong with this, I would be grateful if someone can point out my mistake.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*order.c file*/

  #include <stdio.h>

void ascending2(int *ptr1, int *ptr2){
	
	int temp;
	
	if((*ptr1)>(*ptr2)){
		temp = *ptr1;
		*ptr1 = *ptr2;
		*ptr2 = temp;
	}
}

void ascending3(int *ptr1, int *ptr2, int *ptr3){

	ascending2(ptr1, ptr2);
	ascending2(ptr1, ptr3);
	ascending2(ptr2, ptr3);
}

void descending3(int *ptr1, int *ptr2, int *ptr3){

	ascending3(ptr3, ptr2, ptr1);
}

void order(int a, int b, int c, char d){

	if(d == 'D'){
	printf("ascending3 \n");
	ascending3(&a, &b, &c);
	}
	if(d == 'A'){
	printf("descending3 \n");
	descending3(&a, &b, &c);
	}
}


1
2
3
4
5
6
7
8
9
/*order.h file*/

#include <stdio.h>

void ascending2(int *ptr1, int *ptr2);
void ascending3(int ptr1, int ptr2, int ptr3);
void descending3(int *ptr1, int *ptr2, int *ptr3);
void order(int a, int b, int c, char d);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*user_input.c file*/

#include <stdio.h>
#include "order_4.h"

int main(){

	int a;
	int b;
	int c;
	char d;

	void (*foo)(int, int, int, char);
	foo = &order;

	printf("Input the three int values and the letter A or D: ");
	
	scanf("%d %d %d %c", &a, &b, &c, &d);

	foo(a, b, c, d);

	printf("%d %d %d %c\n", a, b, c, d);

	return 0;
}


As you can see the header file joins the two files so that I can use the function pointer to call the order function from the order.c file to output the result in the user input file, the order.c file is what provides the functions to reorder the values based on the letter A or D.
void (*foo)(int, int, int, char);
The int parameters are passed by value, and therefore the a, b, c of main() cannot possibly change.
Last edited on
Omg, thank you man. finally got it working, to anyone who has similar problem I made these changes:

1
2
3
4
5
6
7
8
9
10
11
void order(int *a, int *b, int *c, char d){

	if(d == 'D'){
	printf("ascending3 \n");
	ascending3(a, b, c);
	}
	if(d == 'A'){
	printf("descending3 \n");
	descending3(a, b, c);
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){

	int a;
	int b;
	int c;
	char d;

	void (*foo)(int*, int*, int*, char);
	foo = &order;

	printf("Input the three int values and the letter A or D: ");
	
	scanf("%d %d %d %c", &a, &b, &c, &d);

	foo(&a, &b, &c, d);

	printf("%d %d %d %c\n", a, b, c, d);

	return 0;
}


Seriously man you are awesome :)
Topic archived. No new replies allowed.