pointers to functions

This is a discussion on pointers to functions within the C Programming forums, part of the General Programming Boards category; Hi guys please guide me in the right direction with pointers to functions where would you use them ect... /* ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    pointers to functions

    Hi guys please guide me in the right direction with pointers to functions where would you use them ect...

    /* In the following code why is a pointer to a function required */
    /* when we could have just created the array in main, called amend */
    /* and from there called display both with array name passed to them */


    #include<stdio.h>
    #define ARRAYSIZE 5

    int *create(void); /* pointer to function */
    void amend(int *i_ptr);
    void display(int *i_ptr);

    void main()
    {
    int *pass_ptr;

    pass_ptr=create(); /* call function return pointer */

    amend(pass_ptr);
    display(pass_ptr);
    }

    int *create(void)
    {
    static int values[ARRAYSIZE];
    int i;

    for(i=0;i<ARRAYSIZE;i++)
    {
    printf("Enter integer value for position %d >",i+1);
    scanf("%d",&values[i]);
    }

    return(values); /* return static pointer */
    }

    void amend(int *i_ptr) /* points to array */
    {
    int i,new_val;

    for(i=0;i<ARRAYSIZE;i++)
    {
    printf("\nEnter new value to store in array position %d >",i+1);
    scanf("%d",&new_val);

    *i_ptr=new_val;
    i_ptr++;
    }
    }

    void display(int *i_ptr)
    {
    int i;

    printf("\nNew values stored in the array are :\n");
    printf("\n\tPosition\t\tValue\n");

    for(i=0;i<ARRAYSIZE;i++)
    printf("\n\t%d\t\t\t%d",i+1,*i_ptr++);
    }

    THANKS FOR THE HELP.
    learner wanting to be a master.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    pointer to functions

    A good situation where you might need a pointer to a function is in initialization... Andre Lamothe explains this really well in his book "Windows Game Programming"
    Say for instance implementation of a fucntion depended of the video card

    Code:
    void SetPixel( int card, int x, int y)
    {
          switch( card )
        {
             case ATI:        SetPixelAti(x,y); break;
    
             case VOODO: SetPixelVoodoo(x,y); break;
    
             case NVIDIA: SetPixelNvidia(x,y); break;
         }
    }
    ..the case would have to be evaluated each time you set a pixel...
    ..the switch is slow... and prone to error... you could also break it
    each time you add a new card.. according to Lamothe
    ...what if instead upon initization you determined what type of video card this system had
    Code:
    void Init(){
      int card = GetVideoCard();
    
      switch( card )
      {
         case ATI: SetPixel = SetPixelAti;              break;
         case VOODO: SetPixel = SetPixelVoodoo; break;
         case NVIDIA: SetPixel = SetPixelNvidia;  break;
      }
    }
    ...now each time you want to set a pixel...

    you just calll SetPixel( x, y ) and it knows which function to use...
    ..certainly the user will not be able to change video cards in the midst of your program....

    This can also work for OS dependent fucntions... for instance some methods work on say .... windows9x and not on NT/XP ...
    zMan

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    You can consider too that the name of a function is by itself a pointer to that function : it points to the first word of memory where is stored the function.
    A pointer to a function will store that address too

    A very simple example :

    #include <stdio.h>

    int realfunction(void)
    {printf("Using function realfunction\n");}

    int main(void)
    {
    int i;
    int (*foo)(void); /*pointer to a function which has
    same return value type and
    same argument types as the function*/
    foo = realfunction;
    for(i=0;i<5;i++)
    foo();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

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