# pointer to function, sorting algorithm done but still parts unclear

This is a discussion on pointer to function, sorting algorithm done but still parts unclear within the C Programming forums, part of the General Programming Boards category; Hello I have this sorting algorithm using pointers to function. Yet, there is one part that I dont see how ...

1. ## pointer to function, sorting algorithm done but still parts unclear

Hello

I have this sorting algorithm using pointers to function. Yet, there is one part that I dont see how it works.(See UPDATE below if you want to see how I found my dumb missing understanding).

When in the main () function there is a call to function 'ordenamiento' (sorting) and this function in turn contains a call to another function (comparacion), the pointer that receives that call to that 'comparacion' where does it actually get the values that it sends to function 'comparacion' so that this can compare if value1 is bigger than value 2. (and return either 1 or -1) I dont see anywhere it picks them from anywhere.

see here

Code:
`void Ordenamiento (int (*funcion)(int , int), int *Vector,int maximo)`
I dont see those int, int getting their values from anywhere so that they are being used by function 'comparacion' that the pointer *funcion points to. (Aside from the fact that I havent seen any assignment that links the pointer to function *funcion to any address, and that is something that has to happen before.

Thanks a lot

Code:
```#include <stdio.h>
#include <conio.h>

// Función para comparar dos valores. Retorna 1 o -1
int Comparacion(int valor1,int valor2)
{
return valor1 > valor2 ? 1 : -1; // where do valor1 and valor 2 are actually getting their values from ? I cant see it
}

// Función para intercambiar el valor de dos posiciones en un Vector
void CambiarDatos(int posI,int posJ,int *Vector)
{
int aux=Vector[posI];
Vector[posI]=Vector[posJ];
Vector[posJ]=aux;
}
// Función para ordenar, recibe un puntero a una función
void Ordenamiento (int (*funcion)(int , int), int *Vector,int maximo)
{
for (int i=0;i<maximo-1;i++)
{
for (int j=i+1;j<maximo;j++)
{
if (funcion(Vector[i],Vector[j])>0)
CambiarDatos(i,j,Vector);
}
}
}

// Función para imprimir un vector
void Imprime_Vector(int *Vector,int maximo)
{
for (int i=0;i< maximo;i++)
printf("-\n",Vector[i]);
}
int main()
{
int Vector[10]={5,76,4,8,50,2,44,7,23,6};
printf("Original: \n");
Imprime_Vector(Vector,10);
Ordenamiento(Comparacion,Vector,10);
Imprime_Vector(Vector,10);

}```

2. It's the simple things which get you sometimes.
In this case, broken printf calls.
Code:
```\$ gcc -std=c99 -Wall bar.c
bar.c: In function ‘Imprime_Vector’:
bar.c:33: warning: too many arguments for format

\$ gcc -std=c99 bar.c
\$ ./a.out
Original:
5
76
4
8
50
2
44
7
23
6
2
4
5
6
7
8
23
44
50
76
\$```

3. Hello Salem

thank you for the attention dedicated. Well yes, I know that when I pasted the call here, I left out the end curly bracket but since it is not executing here I did not mind. Yes, I could not run it either since my compiler (and I can see you have added the code that it needed as per the C99 restriction) could not process it as such. Yet, my questions still remain:

Line 41 calls to line 18, but where does this line 18 get the int and int to send them to line 5 ?

regards

4. Hello Salem

thank you for the attention dedicated. Well yes, I know that when I pasted the call here, I left out the end curly bracket but since it is not executing here I did not mind. Yes, I could not run it either since my compiler (and I can see you have added the code that it needed as per the C99 restriction) could not process it as such. Yet, my questions still remain:

Line 41 calls to line 18, but where does this line 18 get the int and int to send them to line 5 ?

regards

UPDATE,

Ok, now I think I get it. On line 18 we are actually just defining the pointer to function, its type and the arguments but it is not getting anything at this moment or it is not being operative (obviously I am a newbie because I realise what a dumb miss I was doing), it is only on line 24 when the pointer to function operates, and it does by sending the 2 values from the array up to line 5. Now I get it. So matter solved

thanks

5. That would be line 24
Code:
```void Ordenamiento (int (*funcion)(int , int), int *Vector,int maximo)
{
for (int i=0;i<maximo-1;i++)
{
for (int j=i+1;j<maximo;j++)
{
if (funcion(Vector[i],Vector[j])>0)
CambiarDatos(i,j,Vector);
}
}
}```

6. yes, i noted it above i finally got it thanks a lot