LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-25-2006, 02:03 PM   #1
spx2
Member
 
Registered: Dec 2005
Location: focsani-romania
Distribution: debian
Posts: 160

Rep: Reputation: 30
calling a function from a pointer


i have done this
Code:
#include <iostream.h>
void *a[20];
int adunare(){cout<<"AsdA";

return 5;};
void main()
{
 a[0]=adunare;
 cout <<a[0];
 a[0]();

 cin>>int();
 }
i am trying to make an array of pointers to functions
which is in this case a.
i am trying to call a[0] which is actually adunare
by calling a[0] like this : a[0]();
why is it not working and how should it have been done ?
 
Old 05-25-2006, 02:08 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
This should help:
http://www.newty.de/fpt/fpt.html

Here's a simple example (this syntax applicable to either C or C++):
Code:
#include <stdio.h>

typedef int (*fnptr)(int i);

int
a(int i)
{
  printf ("a(%d)= %d...\n", i, i * 2);
  return (i*2);
}

int
b(int i)
{
  printf ("b(%d)= %d...\n", i, i * 10);
  return (i*10);
}

int
main ()
{
   fnptr p = a;
   (*p)(3);

   p = b;
   (*p)(3);
   return 0;
}
The interesting thing about this example is (if you happen to be coding in C++), it really would make more sense if "a" and "b" were each classes (instead of function pointers).

Last edited by paulsm4; 05-25-2006 at 04:54 PM.
 
Old 05-25-2006, 02:47 PM   #3
spx2
Member
 
Registered: Dec 2005
Location: focsani-romania
Distribution: debian
Posts: 160

Original Poster
Rep: Reputation: 30
dear paulsm4 the question is if one can call p(3).
a(3),b(3) we can obviously call.
thank you!
awaiting your answer
 
Old 05-25-2006, 04:52 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Sorry -
Code:
int
main ()
{
   fnptr p = a;
   (*p)(3);

   p = b;
   (*p)(3);
   return 0;
}
Is that what you were looking for?
Quote:
OUTPUT:
a(3)= 6...
b(3)= 30...
Again, this is a great tutorial:
http://www.newty.de/fpt/fpt.html

Last edited by paulsm4; 05-25-2006 at 05:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling function in kernel vishalbutte Programming 0 02-14-2006 07:34 AM
Function pointers, calling, and templates The_Nerd Programming 2 10-09-2005 07:08 PM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
switch statements calling a function tekmorph Programming 2 10-19-2004 05:53 PM
Calling my function through MACROS in OpenOfficeWriter mcp_achindra Linux - Software 0 07-05-2004 06:56 AM


All times are GMT -5. The time now is 03:14 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration