Calling a Pointer?

Discussion in 'C Programming' started by Daniel Rudy, May 15, 2004.

  1. Daniel Rudy

    Daniel Rudy Guest

    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.
     
    Daniel Rudy, May 15, 2004
    #1
    1. Advertising

  2. Daniel Rudy

    -berlin.de Guest

    Daniel Rudy <.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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 15, 2004
    #2
    1. Advertising

  3. Daniel Rudy <.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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "There's no business like slow business."
    - Tailgunner
     
    Joona I Palaste, May 15, 2004
    #3
  4. In 'comp.lang.c', Daniel Rudy <.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/
     
    Emmanuel Delahaye, May 15, 2004
    #4
  5. Emmanuel Delahaye, May 15, 2004
    #5
  6. Daniel Rudy

    Daniel Rudy Guest

    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.
     
    Daniel Rudy, May 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    742
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    876
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,330
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    657
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    497
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page