Struct of pointer-to-function

A

Andrej Prsa

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
 
M

Mark A. Odell

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?
 
A

Andrej Prsa

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
 
V

Victor Nazarov

Andrej said:
>
>
>
> 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;
}
 
R

Robert Bachmann

Andrej said:
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;
}
 
A

Andrej Prsa

Hi!
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
 

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,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top