Thread: passing arguments to a function

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    passing arguments to a function

    Hi folks!

    The code below is from an old C textbook that I'm working through.

    I can see the function intcmp called within qsort and bsearch, but I can't see any arguments passed to it. So how is the intcmp function getting values for v1 and v2?

    Thanks in advance!

    Code:
    /* Using qsort() and bsearch() with values.*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 10
    
    int intcmp(const void *v1, const void *v2);
    
    int main( void )
    {
         int arr[MAX], count, key, *ptr;
    
         /* Enter some integers from the user. */
    
         printf("Enter %d integer values; press Enter after each.\n", MAX);
    
         for (count = 0; count < MAX; count++)
             scanf("%d", &arr[count]);
    
         puts("Press Enter to sort the values.");
         getc(stdin);
    
         /* Sort the array into ascending order. */
    
         qsort(arr, MAX, sizeof(arr[0]), intcmp);
    
         /* Display the sorted array. */
    
         for (count = 0; count < MAX; count++)
             printf("\narr[%d] = %d.", count, arr[count]);
    
         puts("\nPress Enter to continue.");
         getc(stdin);
    
         /* Enter a search key. */
    
         printf("Enter a value to search for: ");
         scanf("%d", &key);
    
         /* Perform the search. */
    
         ptr = (int *)bsearch(&key, arr, MAX, sizeof(arr[0]),intcmp);
    
         if ( ptr != NULL )
             printf("%d found at arr[%d].", key, (ptr - arr));
         else
             printf("%d not found.", key);
         return 0;
    }
    
    int intcmp(const void *v1, const void *v2)
    {
         return (*(int *)v1 - *(int *)v2);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    Quote Originally Posted by cfanatic
    I can see the function intcmp called within qsort and bsearch, but I can't see any arguments passed to it.
    Yes, because a pointer to intcmp is passed to qsort.

    Quote Originally Posted by cfanatic
    So how is the intcmp function getting values for v1 and v2?
    qsort passes those arguments when it calls intcmp using the function pointer you passed to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Thanks laserligt!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structures as Function Arguments
    By Jyqft in forum C Programming
    Replies: 13
    Last Post: 03-26-2012, 08:02 AM
  2. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  3. Passing arguments to another function
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 12-24-2006, 05:57 AM
  4. need function help, passing arguments
    By infernosnow in forum Game Programming
    Replies: 18
    Last Post: 07-18-2006, 02:45 AM
  5. Passing arguments to function...
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 12:50 PM