Like Tree1Likes
  • 1 Post By Salem

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. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    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);
        printf("Ordenado: \n");
        Imprime_Vector(Vector,10);
    
    }
    Last edited by alvarito; 10-02-2011 at 11:12 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    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
    Ordenado: 
    2
    4
    5
    6
    7
    8
    23
    44
    50
    76
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    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. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    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
    Last edited by alvarito; 10-02-2011 at 11:10 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    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);
            }
        }
    }
    alvarito likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    yes, i noted it above i finally got it thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Algorithm Problem
    By Khadafi in forum C++ Programming
    Replies: 12
    Last Post: 02-18-2011, 03:20 PM
  2. unclear about typecasting and creating pointers
    By kirani in forum C Programming
    Replies: 11
    Last Post: 06-16-2010, 04:29 PM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  4. sorting algorithm
    By RazielX in forum C Programming
    Replies: 4
    Last Post: 05-04-2004, 06:20 PM
  5. 'Sorting' algorithm
    By Dual-Catfish in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2001, 01:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21