Function pointers - why, when can I get around?

Disclaimer: I have read countless other articles on this topic, and I still do not receive them. Example: why do this:

void func(int a, void (*callback)(int))
{
    /* do something with a and callback */
    callback(3);
}

void pointme(int b)
{
    /* do something with b */
}

int main()
{
    void (*pf)(int);
    pf = &pointme;
    func(10, pf);
}

when i can just do this:

void func(int a)
{
    pointme(3);
    /* do something with a*/
}

void pointme(int b)
{
    /* do something with b */
}

int main()
{
    func(10);
}

??? I really do not understand. Any help would be greatly appreciated. Thank!!!

+4
source share
3 answers

when can i just do it [...]

, , . , , , , . , , , .

, , , , .

: , .

, . :

  • , ,
  • , .
+6

C :

  • ;
  • "" ;

, .

- , , .., , , - ; , , , . (.dll Windows, .so Linux). , , .

, , , :

/**
 * lib1.c
 *
 * Provides functions that are called by another program
 */
#include <stdio.h>

static char *names[] = {"func1", "func2", NULL};

char **getNames( void ) { return names; }
void func1( void )      { printf( "called func1\n" ); }
void func2( void )      { printf( "called func2\n" ); }

getNames , , ( , ).

, :

gcc -o lib1.so -std=c99 -pedantic -Wall -Werror -fPIC -shared lib1.c

lib1.so.

:

#include <stdio.h>
#include <dlfcn.h>

int main( void )
{
  /**
   * Open the shared library file
   */
  void *lib1handle = dlopen( "lib1.so", RTLD_LAZY | RTLD_LOCAL );

  /**
   * Load the "getNames" function into the current process space
   */
  char **(*libGetNames)( void ) =  dlsym( lib1handle, "getNames" );
  if ( libGetNames )
  {
    /**
     * call the "getNames" function in the shared library
     */
    char **names = libGetNames();
    while ( names && *names)
    {
      printf( "calling %s\n", *names );
      /**
       * Load each named function into the current process space
       * and execute it
       */
      void (*func)(void) =  dlsym( lib1handle, *names++ );
      if ( func )
        func();
    }
  }
  dlclose( lib1handle );
  return 0;
}

:

gcc -o main -std=c99 -Wall -Werror main.c -ldl

, lib1.so ; main , .

LD_LIBRARY_PATH, dlopen :

[fbgo448@n9dvap997]~/prototypes/dynlib: export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

- getNames , . libGetNames getNames , func func1 func2 . :

[fbgo448@n9dvap997]~/prototypes/dynlib: ./main
calling func1
called func1
calling func2
called func2

, ? , , Photoshop Audacity, - ; , , .

, main , , main main,

""

"" C qsort. qsort, ; , , , . , :

#include <stdio.h>
#include <stdlib.h>

int cmpInt( const void *lhs, const void *rhs )
{
  const int *l = lhs, *r = rhs;
  return *l - *r;
}

int cmpFloat( const void *lhs, const void *rhs )
{
  const float *l = lhs, *r = rhs;
  return *l - *r;
}

char *fmtInt( char *buffer, size_t bufsize, const void *value )
{
  const int *v = value;
  sprintf( buffer, "%*d", (int) bufsize, *v );
  return buffer;
}

char *fmtFloat( char *buffer, size_t bufsize, const void *value )
{
  const float  *v = value;
  sprintf( buffer, "%*.*f", (int) bufsize, 2, *v );
  return buffer;
}

void display( const void *data, size_t count, size_t size, char *(*fmt)(char *, size_t, const void *))
{
  const char *d = data;
  char buffer[10];
  printf( "{%s", fmt( buffer, sizeof buffer, &d[0] ));
  for ( size_t i = size; i < count * size; i += size )
    printf( ", %s", fmt( buffer, sizeof buffer, &d[i] ));
  printf( "}\n" );
}

int main( void )
{
  int   iarr[] = {9, 100, 53, 99, 4, 29, 44};
  float farr[] = {9, 100, 54, 99, 4, 29, 44};

  printf( "iarr before sort: " );
  display( iarr, sizeof iarr / sizeof *iarr, sizeof *iarr, fmtInt );
  qsort( iarr, sizeof iarr / sizeof *iarr, sizeof *iarr, cmpInt );
  printf (" iarr after sort: " );
  display( iarr, sizeof iarr / sizeof *iarr, sizeof *iarr, fmtInt );

  printf( "farr before sort: " );
  display( farr, sizeof farr / sizeof *farr, sizeof *farr, fmtFloat );
  qsort( farr, sizeof farr / sizeof *farr, sizeof *farr, cmpFloat );
  printf (" farr after sort: " );
  display( farr, sizeof farr / sizeof *farr, sizeof *farr, fmtFloat );

  return 0;
}

, - : int float, , , :

[fbgo448@n9dvap997]~/prototypes/dynlib: ./sorter
iarr before sort: {         9,        100,         53,         99,          4,         29,         44}
 iarr after sort: {         4,          9,         29,         44,         53,         99,        100}
farr before sort: {      9.00,     100.00,      54.00,      99.00,       4.00,      29.00,      44.00}
 farr after sort: {      4.00,       9.00,      29.00,      44.00,      54.00,      99.00,     100.00}

. qsort , , "" "" . cmpInt cmpFloat ; . (sort_int, sort_float, sort_foo); qsort.

, display , { }. fmtInt fmtFloat int float. - .

, , , "" . , void *, , . ; ( ). , ++ Java #, , , (.. , ).

, , .

+2

, qsort.

void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));

- .

, , () , , .

qsort . . , , , .

+2

All Articles