Results 1 to 12 of 12

Thread: Function pointers

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Function pointers

    As of yet, I see no use in function pointers. I cannot get it to work. Can anyone figure out why? I am trying to learn how to use them just in case. But just please let me know if they are useless of not.

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    char funk1(char[], const int, char (*)(char, char));
    char funk2(char[], char);

    int main() {
    const int size=100; int h=2;
    char g[size]="Hi";
    char n[size]={0};

    cin.getline (g, size);
    cin.getline (n, size);
    funk1(g, size, funk2);

    system("PAUSE");
    return 0;
    }

    char funk1(char gg[], const int asize, char (*p)(char, char)) {

    (*p)(char gg[0], char asize);
    }

    char funk2(char x[], char y) {
    if (strlen(x)<8)
    cout<< "xless";
    if (y<8)
    cout<< "yless";
    }
    Last edited by aewarnick; Jan 18th, 2003 at 10:50 PM.

  2. #2

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Nevermind, I figured it out. But I still don't see any need for function pointers. Is there?

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by jim mcnamara here
    Other than callbacks:
    A lot of standard C library routines use them. qsort() for instance.
    They allow a kind of generalization you cannot get any other way.

    They are also used extensively when writing parsers. OS functions also use them - those numbers you see in aliases in API declarations in VB are offsets into a masthead (array) of function pointers.

    -- This is not in defense of them 'cause they can be a colossal pain to work with.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thank you.

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Function pointers are useful and essential for programming event-oriented applications, eg windows apps.

    In plain old structured programming, your program will call all its functions. In Windows, it's different, Windows OS has to call your handler (or window proc) when an event occurs, so it got to have the address of the functions, in order to call them.

    Maybe you would want to search the C++ forum for 'CALLBACK FUNCTIONS' and 'function pointers'; CornedBee had explained them in detail.

  6. #6

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thank you trans... for that explanation.

  7. #7
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    This is considered intermediate level C programming with function pointers.

    Code:
    #include <stdlib.h>
    #define F FUNC
    /* prototypes for 4 functions that return float values */
    float  f0(float );
    float  f1(float );
    float  f2(float );
    float  f3(float );
    /* create a way to define functions */
    typedef float (*F)(float );
    
    float  f0(float  i){return (i>0)? i : (-1)*i ;} 
    float  f1(float i){ return i+i;} 
    float  f2(float i){return i*i;} 
    float  f3(float i){return i/2;}
    int main(){
            float i;
            {F a[5];  ./* declare an array of function pointers */
              {a[0]=f0;a[1]=f1; a[2]=f2; a[3]=f3; i=(-15037); /* initialize them */
                 {int j; 
                  for(j=1;j<4;j++)printf("%.0f. %.4f\n",i, (a[j])((a[j-1])(i))); /* play */
                 }
              }
            }
    	return 0;
    }
    /* this code was created for a class I taught */

  8. #8

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thank you.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Why the many braces?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    It was demo code - showing localized variables in a function.

    It isn't meant to be practical or readable, and not very useful either.

  11. #11
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    i wonder if cornedbee remembers almost a year ago helping me with function pointers being passed between classes?

    my use may not be useful to other people, but i thought it was cool

    basically it was a whole menu system where you setup the caption, the input options, and the functions that would be called based upon what choice the user made
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by DNA7433
    i wonder if cornedbee remembers almost a year ago helping me with function pointers being passed between classes?

    my use may not be useful to other people, but i thought it was cool

    basically it was a whole menu system where you setup the caption, the input options, and the functions that would be called based upon what choice the user made
    I faintly recall it. But you see, of my nearly 4000 posts on VBF at least 3500 are in the C++ forum, so I don't remember most of them. But I do remember something of yours because I had to try it out myself.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width