Calling a Pointer?

D

Daniel Rudy

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);
}
 
J

Jens.Toerring

Daniel Rudy said:
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 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
 
J

Joona I Palaste

Daniel Rudy said:
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]();
 
E

Emmanuel Delahaye

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'.
 
D

Daniel Rudy

And somewhere around the time of 05/15/2004 02:53, the world stopped and
listened as Daniel Rudy contributed the following to humanity:

Thanks to everyone who replied. It was greatly helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top