473,761 Members | 4,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create an initialise pointer to pointer to function variable;

HI,

I have a function as

void f(int p)
{
return p++;
}

now I have created a function pointer as
void(**pf)(int) ;

and initialization as
*pf=f;

but compiler gives error...

Why ?

a.a.cpp

Jul 23 '05 #1
9 2524
* iceColdFire:

I have a function as

void f(int p)
{
return p++;
}
First, you cannot return non-void when the function is declared
as returning void.

Second, with 'void' changed to 'int', that has the same final effect as

int f( int p )
{
return p;
}

You might want to take a look at

<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02 _11.html>

for other reasons why you should avoid postfix increment and decrement.

now I have created a function pointer as
void(**pf)(int) ;

and initialization as
*pf=f;

but compiler gives error...


The compiler shouldn't flag _that_ as an error, but a good compiler will
warn you that you're derefencing an uninitialized pointer.

Possibly what you wanted was

int (*pf)(int) = f;

However, as a beginner try to avoid using pointers directly. In this case
you would probably (here I'm guessing, but probably) be much better served
by using a virtual member function instead. Try to read up on that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Hi Alf::

Well...that was great explanaiton indeed...howeve r I need to use
pointer to pointer to function as

int(**pf)(int);

Now thing is how do I initialise the variable pf with f, and is my
variable declaration correct for (pointer to (pointer to function))

Thanks,
a.a.cpp

Jul 23 '05 #3
* iceColdFire:

Well...that was great explanaiton indeed...howeve r I need to use
pointer to pointer to function as

int(**pf)(int);

Now thing is how do I initialise the variable pf with f
int (*pf)(int) = f;
int (**ppf)(int) = &pf;
and is my
variable declaration correct for (pointer to (pointer to function))


I think so (modulo bad eyesight & auto filtering of on-screen text).

But if you'd describe what you're trying to achieve instead of how you're
intending to implement part of the solution, I'm reasonably sure that I or
someone else can tell you how to do that much more safely and easily via
virtual member functions -- or perhaps templates, or whatever.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
"iceColdFir e" <ic*******@yaho o.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com
Hi Alf::

Well...that was great explanaiton indeed...howeve r I need to use
pointer to pointer to function as

int(**pf)(int);

Now thing is how do I initialise the variable pf with f, and is my
variable declaration correct for (pointer to (pointer to function))

Thanks,
a.a.cpp

An alternative to Alf's scheme that is closer to your original is as follow:

// pointer to function
int(*pf)(int);

// pointer to pointer to function
int(**ppf)(int) ;

int main()
{
//initialise double pointer to point to single pointer
ppf=&pf;
// use double pointer to initialise single pointer
*ppf = &f;
// call function from double pointer
int x = (**ppf)(5);
return 0;
}

Note that the & in

*ppf = &f;

is optional as is one of the asterisks in

int x = (**ppf)(5);

but it is more logical to include them (if working with pointers to member
functions, then you must include them --- you don't get a choice).
--
John Carson

Jul 23 '05 #5
::Alf

Allright,
That was nice and neat...can we initialize ppf with f instead of
initialising with pf...

And here is the explanation, I intend to build an array of pointer to
pointer to functions which shall be used in a module for runtime
function reference...I am avoiding using templates and virtual member
functions, as what I have as resource is just the function addresses
and not the code...
so I can only use pointers to build up the entire system....

Thanks,
a.a.cpp

Jul 23 '05 #6
::John,,,thanks for the nice explanation...
Check my last posting for what I actually intend to build..

a.a.cpp

Jul 23 '05 #7
On 2005-05-23, iceColdFire <ic*******@yaho o.com> wrote:
::Alf

Allright,
That was nice and neat...can we initialize ppf with f instead of
initialising with pf...

And here is the explanation, I intend to build an array of pointer to
pointer to functions which shall be used in a module for runtime
function reference...
Yes, fine -- but why do you need pointer to (pointer to functions) ? Why is
a pointer to function not good enough ?

Or do you mean that you want to create a dynamic array of pointer-to-functions
and the type of that dynamic array is pointer to (pointer to function) ?
I am avoiding using templates and virtual member
functions, as what I have as resource is just the function addresses
and not the code...


You can wrap a function pointer in a function object (for example). Most of the
solutions that would be suggested do not require you to have access to the source
code for your functions.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #8
::Donovan,
Yup !!!.

I intend to build a dynamic array of [......]
Also I liked your wrapping idea ...means wrapping a function pointer
into a function object...
But how do you do that....do you have any related docs for the trick..

Thanks,...
a.a.cpp

Jul 23 '05 #9
"iceColdFir e" <ic*******@yaho o.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com
John,,,thanks for the nice explanation...

Check my last posting for what I actually intend to build..


If you want a dynamic array of function pointers, then you can build it with
new or with vector or...

With vector, you can just go:

// creates a 10 element vector of function pointers
std::vector<int (*)(int)> vec(10);

//Then you can set

vec[0] = &f;

// and so on. You would call the function from
// the first vector element as follows:

int y = (*vec[0])(7);

Naturally, you can expand the size of the vector using push_back or various
other member functions.

--
John Carson

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5478
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can initialize the static object without invoking the constructor? Below is the sample coding. Header file ASURegistrationManager.h #include "ASURegistration.h"
7
8869
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
6
5436
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. I know if I change char name this will work, but I will waste alot of space. (I am learning so I want to learn the best way) How can I define a struct the allows variable length character strings in the
52
5655
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every time I get the textbooks out end up throwing them against the wall in rage. Thats been going on for 10 years now. Anyway, I have: typedef struct _record { int age;
2
1763
by: Tomás | last post by:
Up until lately I've been writing all mad kinds of code for accomplishing things, but lately I've decided to lean more toward the whole readability, etc. side of things. I have a command line application, which I intend to be used like so: unicon.exe -8to32 source.txt target.txt There are three command line arguments. The first is something like:
27
8972
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in advance. Erik
51
4467
by: Kuku | last post by:
What is the difference between a reference and a pointer?
23
7416
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
4
12440
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.