Struct of pointer-to-function

Discussion in 'C Programming' started by Andrej Prsa, Mar 2, 2004.

  1. Andrej Prsa

    Andrej Prsa Guest

    Hi!

    I'm trying to create a small scientific script language; my idea was to
    create a struct with the corresponding global variable like this:

    typedef struct commands
    {
    int recno;
    int *argc;
    char **name;
    int *(*func)();
    } commands;

    commands command;

    Then I thought I could create a function that would add commands to this
    global variable:

    int add_command (char *name, int argc, int func() )
    {
    /* recno has been set to 0 and all pointers in command set to NULL */
    /* elsewhere in the program. */

    int i = command.recno++;

    command.name = realloc (command.name, i * sizeof (*char));
    command.argc = realloc (command.argc, i * sizeof (*int));
    command.func = realloc (command.func, i * sizeof (*int));

    command.name = strdup (name);
    command.argc = argc;
    command.func = func;

    return 0;
    }

    You guys can probably see from miles away that I don't know how to assign
    a pointer-to-function value to command.func; the most logical thing for me
    would be to use something like:

    command.(*func)() = ...

    but it doesn't work. So please help, how do I do this?

    Thanks,

    Andrej
     
    Andrej Prsa, Mar 2, 2004
    #1
    1. Advertising

  2. Andrej Prsa <> wrote in
    news::

    Start by changing the sizeof's to sizeof (char *), the * follows the type
    in this case

    > command.name = realloc (command.name, i * sizeof (*char));
    > command.argc = realloc (command.argc, i * sizeof (*int));
    > command.func = realloc (command.func, i * sizeof (*int));
    >
    > command.name = strdup (name);


    strdup()? Is that part of standard C?

    --
    - Mark ->
    --
     
    Mark A. Odell, Mar 2, 2004
    #2
    1. Advertising

  3. Andrej Prsa

    Andrej Prsa Guest

    Hi!

    > Start by changing the sizeof's to sizeof (char *), the * follows the
    > type in this case


    Sorry, sorry, that was a typo; in reality my struct is much longer and I
    didn't want to flood the newsgroup with too much of everything, that's why
    I only took the relevant (simplified) parts out of my program. Hence the
    trivial mistake. Sorry again!

    > strdup()? Is that part of standard C?


    Errr, isn't it? Declared in string.h?

    Best wishes,

    Andrej
     
    Andrej Prsa, Mar 2, 2004
    #3
  4. Andrej Prsa

    Default User Guest

    Andrej Prsa wrote:

    > > strdup()? Is that part of standard C?

    >
    > Errr, isn't it? Declared in string.h?



    Nope. It's a common extension, but not standard.



    Brian Rodenborn
     
    Default User, Mar 2, 2004
    #4
  5. Andrej Prsa wrote:

    >
    >>strdup()? Is that part of standard C?

    >
    >
    > Errr, isn't it? Declared in string.h?
    >


    It is not.

    The solution is:

    typedef struct commands
    {
    int recno;
    int *argc;
    char **name;
    int *(**func)(); /* Pointer to pointer to function returning pointer
    to int. I can suppose that you want pointers to functions returning int,
    not pointer to int, so the declaretion must be int (**func)()*/
    } commands;

    commands command; /* Why dont you have array of structs instead struct
    of arrays? The first is easier to use... */

    int add_command (char *name, int argc, int *(*func)() /* You can choose
    int (*func)() )
    {
    /* recno has been set to 0 and all pointers in command set to NULL */
    /* elsewhere in the program. */

    int i = command.recno++;

    command.name = realloc (command.name, i * sizeof (command.name[0]));
    command.argc = realloc (command.argc, i * sizeof (command.argc[0]));
    command.func = realloc (command.func, i * sizeof (command.func[0]));
    /* Checks for results are required */


    command.name = strdup (name);
    /* Really strdup is illegal because it's name starts with str */
    command.argc = argc;
    command.func = func;

    return 0;
    }
     
    Victor Nazarov, Mar 2, 2004
    #5
  6. strdup() (was Re: Struct of pointer-to-function)

    Andrej Prsa wrote:
    >>strdup()? Is that part of standard C?

    >
    > Errr, isn't it? Declared in string.h?


    No it's not although I think it's a very useful function.
    However it's easy to make an portable replacement.

    #include <stdlib.h>
    #include <assert.h>

    char* dupstr(const char* original)
    {
    char* p;

    assert(original != NULL);
    if (p=malloc(strlen(original)+1))
    p=strcpy(p, original);

    return p;
    }

    --
     
    Robert Bachmann, Mar 2, 2004
    #6
  7. Andrej Prsa

    Andrej Prsa Guest

    Re: strdup() (was Re: Struct of pointer-to-function)

    Hi!

    > Andrej Prsa wrote:
    > >>strdup()? Is that part of standard C?

    > >
    > > Errr, isn't it? Declared in string.h?

    >
    > No it's not although I think it's a very useful function.
    > However it's easy to make an portable replacement.
    >
    > #include <stdlib.h>
    > #include <assert.h>
    >
    > char* dupstr(const char* original)
    > {
    > char* p;
    >
    > assert(original != NULL);
    > if (p=malloc(strlen(original)+1))
    > p=strcpy(p, original);
    >
    > return p;
    > }


    Thanks very much for pointing that out!

    Best wishes,

    Andrej
     
    Andrej Prsa, Mar 2, 2004
    #7
  8. Re: strdup() (was Re: Struct of pointer-to-function)

    Robert Bachmann wrote:
    > #include <stdlib.h>
    > #include <assert.h>


    Ups, I forgot to
    #include <string.h>

    > char* dupstr(const char* original)
    > {
    > char* p;
    >
    > assert(original != NULL);
    > if (p=malloc(strlen(original)+1))
    > p=strcpy(p, original);
    >
    > return p;
    > }


    --
     
    Robert Bachmann, Mar 5, 2004
    #8
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,445
    Chris Fogelklou
    Apr 20, 2004
  2. Ole
    Replies:
    4
    Views:
    623
    Michael Wojcik
    Oct 26, 2004
  3. beetle
    Replies:
    2
    Views:
    972
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    686
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    514
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page