Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Impossible pointer to function assignment?

Status
Not open for further replies.

FransJ

Programmer
Oct 2, 2001
4
NL
(How) can I let a "pointer to a function" variable point to a function that is specified by another variable?

Example:
char rec_value[20];
int (*rcv_mail[2])(char *msg);
extern int ser_rcv_mail(char *msg);
strcpy (rec_value,"ser_rcv_mail");

Is it possible to let rcv_mail[0] point to the function ser_rcv_mail() by using the contents of the rec_value variable, something equal to "rcv_mail[0] = ser_rcv_mail;"?
 
I think...
rcv_mail[0] = ser_rcv_mail;
...should work fine. But you can't use the contents of the rec_value variable since its just a character array, and the compiler isn't going to look at it in any other sense.

By the way, "(How) can I let a "pointer to a function" variable point to a function that is specified by another variable?", you can't specify a function by some variable except by pointers. I think.
Ankan.

Please do correct me if I am wrong. s-)
 
Ankan,

You think a lot!! ::) But u r right ;-)

FransJ, what u r tryin isnt really possible as it would be mere a string of characters not a function name...

Chow!

Roy
user.gif
 
Ok Ankan and Roy,

I was afraid you were going to write this :-(, but (of course) thanks for your help. :)


Frans
 
Hehe..... one question, how to do pointer to a function into a c++ class?

i tried:

class lala
{
int f1(int, int);
int f2(int, int);
int (lala::*pf)(int, int);
};

int lala::f1(int a, int b)
{
return a+b;
}

int lala::f2(int a, int b)
{
return a-b;
}

void main()
{
lala l;
int a = 5, b = 6, c, d;

l.pf = l.f1;

c = l.pf(a, b); //ERR: term does not evaluate to a function

l.pf = l.f2;

d = l.pf(a, b); //ERR: term does not evaluate to a function

printf("Res: %d %d\n", c, d);
}


please send me answer to lorien@postmaster.co.uk

TXS!!!!
 
hi lorien

run this code

#include<iostream.h>

class lala
{
public:

lala()
{
pf[0]=lala::f1;
pf[1]=lala::f2;
};
int choice(int,int,int);
int f1(int, int);
int f2(int, int);
int (lala::*pf[2])(int, int);
};

int lala::f1(int a, int b)
{
return a+b;
}

int lala::f2(int a, int b)
{
return a-b;
}



int lala::choice(int count,int a=0,int b=0)
{

return (this->*pf[count])(a,b);
}



void main()
{
lala l;
int a = 5, b = 6, c, d;

c=l.choice(0,a,b);
d=l.choice(1,a,b);
printf(&quot;Res: %d %d\n&quot;, c, d);
}
hope this helps

bye
yogesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top