PDA

Click to See Complete Forum and Search --> : Function pointers


aewarnick
Jan 18th, 2003, 08:12 PM
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";
}

aewarnick
Jan 18th, 2003, 09:58 PM
Nevermind, I figured it out. But I still don't see any need for function pointers. Is there?

crptcblade
Jan 18th, 2003, 11:08 PM
Originally posted by jim mcnamara here (http://www.vbforums.com/showthread.php?s=&threadid=184227&highlight=function+pointers)
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.

aewarnick
Jan 19th, 2003, 09:21 AM
Thank you.

transcendental
Jan 19th, 2003, 11:13 PM
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.

aewarnick
Jan 22nd, 2003, 08:07 AM
Thank you trans... for that explanation.

jim mcnamara
Jan 22nd, 2003, 02:25 PM
This is considered intermediate level C programming with function pointers.


#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 */

aewarnick
Jan 30th, 2003, 09:10 AM
Thank you.

CornedBee
Jan 30th, 2003, 12:28 PM
Why the many braces?

jim mcnamara
Jan 30th, 2003, 02:25 PM
It was demo code - showing localized variables in a function.

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

DNA7433
Jan 30th, 2003, 04:07 PM
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

CornedBee
Jan 30th, 2003, 05:18 PM
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.