|
-
Jan 18th, 2003, 09:12 PM
#1
Thread Starter
Frenzied Member
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.
-
Jan 18th, 2003, 10:58 PM
#2
Thread Starter
Frenzied Member
Nevermind, I figured it out. But I still don't see any need for function pointers. Is there?
-
Jan 19th, 2003, 12:08 AM
#3
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
-
Jan 19th, 2003, 10:21 AM
#4
Thread Starter
Frenzied Member
-
Jan 20th, 2003, 12:13 AM
#5
Hyperactive Member
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.
-
Jan 22nd, 2003, 09:07 AM
#6
Thread Starter
Frenzied Member
Thank you trans... for that explanation.
-
Jan 22nd, 2003, 03:25 PM
#7
Frenzied Member
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 */
-
Jan 30th, 2003, 10:10 AM
#8
Thread Starter
Frenzied Member
-
Jan 30th, 2003, 01:28 PM
#9
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.
-
Jan 30th, 2003, 03:25 PM
#10
Frenzied Member
It was demo code - showing localized variables in a function.
It isn't meant to be practical or readable, and not very useful either.
-
Jan 30th, 2003, 05:07 PM
#11
Fanatic Member
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.
-
Jan 30th, 2003, 06:18 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|