Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Calling a Pointer?

Reply
Thread Tools

Calling a Pointer?

 
 
Daniel Rudy
Guest
Posts: n/a
 
      05-15-2004

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.
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-15-2004
Daniel Rudy <(E-Mail Removed).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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      05-15-2004
Daniel Rudy <(E-Mail Removed).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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"There's no business like slow business."
- Tailgunner
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-15-2004
In 'comp.lang.c', Daniel Rudy <(E-Mail Removed).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/
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-15-2004
In 'comp.lang.c', (E-Mail Removed)-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/
 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      05-16-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ calling java calling c++ ... Klaus Friese C++ 7 08-15-2005 09:23 PM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Calling FormsAuthentication.SignOut() after calling Response.Flush =?Utf-8?B?TWFydGluIExlZQ==?= ASP .Net 1 09-28-2004 12:47 PM
Server Side button calling page_load before calling it's own click event. Ryan Ternier ASP .Net 4 07-29-2004 01:06 PM



Advertisments