473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Calling a Pointer?


Hello,

How does one call a pointer? Basically, what I would like to do is
have an array of pointers so that a value of a variable in a struct will
act as an index to the array, which contains the addresses of routines.
How does one do this in C? Can it be done? I'm still new to C, so
some of the code below will not be valid...Like the pointer type.

#include <stdio.h>

pointer array[3];
int x;

int routine1()
{
printf("This is routine 1\n");
return(0);
}

int routine2()
{
printf("This is routine 2\n");
return(0);
}

int routine3()
{
printf("This is routine 3\n");
return(0);
}

int main()
{
array[0] = addressof(routine1);
array[1] = addressof(routine2);
array[2] = addressof(routine3);
x = 2;
call(array[x]);
return(0);
}

--
Daniel Rudy

Remove nospam, invalid, and 0123456789 to reply.
Nov 14 '05 #1
5 1271
Daniel Rudy <dc****@invalid.pacbell.nospam.net.0123456789> wrote:
How does one call a pointer? Basically, what I would like to do is
have an array of pointers so that a value of a variable in a struct will
act as an index to the array, which contains the addresses of routines.
How does one do this in C? Can it be done? I'm still new to C, so
some of the code below will not be valid...Like the pointer type. #include <stdio.h> pointer array[3];
Make that

int ( * array[ 3 ] )( void );

That way you get an array with 3 elements, with its element being pointers
to int returning functions that take no arguments (but you should make
that a global variable unless you really need ;-)
int x; int routine1()
{
printf("This is routine 1\n");
return(0);
} int routine2()
{
printf("This is routine 2\n");
return(0);
} int routine3()
{
printf("This is routine 3\n");
return(0);
} int main()
{
array[0] = addressof(routine1);
Much too complicated, make that

array[0] = routine1;

etc.
array[1] = addressof(routine2);
array[2] = addressof(routine3);
x = 2;
call(array[x]);
And use here just

array[2]( );
return(0);
}

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Daniel Rudy <dc****@invalid.pacbell.nospam.net.0123456789> scribbled the following:
Hello, How does one call a pointer? Basically, what I would like to do is
have an array of pointers so that a value of a variable in a struct will
act as an index to the array, which contains the addresses of routines.
How does one do this in C? Can it be done? I'm still new to C, so
some of the code below will not be valid...Like the pointer type. #include <stdio.h> pointer array[3];
int x; int routine1()
{
printf("This is routine 1\n");
return(0);
} int routine2()
{
printf("This is routine 2\n");
return(0);
} int routine3()
{
printf("This is routine 3\n");
return(0);
} int main()
{
array[0] = addressof(routine1);
array[1] = addressof(routine2);
array[2] = addressof(routine3);
x = 2;
call(array[x]);
return(0);
}


Look up function pointers. Basically what you first want to do is:
typedef int (*pointer)();
and then your addressof and call routines diminish to:
array[0] = routine;
and:
array[x]();

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"There's no business like slow business."
- Tailgunner
Nov 14 '05 #3
In 'comp.lang.c', Daniel Rudy <dc****@invalid.pacbell.nospam.net.0123456789>
wrote:
How does one call a pointer? Basically, what I would like to do is
have an array of pointers so that a value of a variable in a struct will
act as an index to the array, which contains the addresses of routines.
How does one do this in C? Can it be done? I'm still new to C, so
some of the code below will not be valid...Like the pointer type.


<snip pseudo-code>

You need to open your C-book and read about 'pointers to functions'.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #4
In 'comp.lang.c', Je***********@physik.fu-berlin.de wrote:
Make that

int ( * array[ 3 ] )( void );


Sounds ugly to me. What about:

typedef int fun_f (void);

<...>

fun_f * array[3];

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #5
And somewhere around the time of 05/15/2004 02:53, the world stopped and
listened as Daniel Rudy contributed the following to humanity:
Hello,


Thanks to everyone who replied. It was greatly helpful.
--
Daniel Rudy

Remove nospam, invalid, and 0123456789 to reply.
Nov 14 '05 #6

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

Similar topics

4
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage...
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
5
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? ...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
13
by: santosh | last post by:
Hi, If I call free() with a uninitialised pointer, will the program state become undefined, or will free() return harmlessly? Incidentally, is there a way in Standard C to determine weather a...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
13
by: Mangabasi | last post by:
Howdy, I have been trying to call the following Fortran function from Python (using Windows XP, Compaq Fortran and Python 2.4). I tried F2Py, Pyfort and calldll with no success. I think I...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.