473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Struct of pointer-to-function

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[i] = strdup (name);
command.argc[i] = argc;
command.func[i] = 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)()[i] = ...

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

Thanks,

Andrej
Nov 14 '05 #1
7 1565
Andrej Prsa <an*********@guest.arnes.si> wrote in
news:20040302172741.5c52740d.an*********@guest.arn es.si:

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[i] = strdup (name);


strdup()? Is that part of standard C?

--
- Mark ->
--
Nov 14 '05 #2
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
Nov 14 '05 #3
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[i] = strdup (name);
/* Really strdup is illegal because it's name starts with str */
command.argc[i] = argc;
command.func[i] = func;

return 0;
}


Nov 14 '05 #4
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
Nov 14 '05 #5
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;
}

--
Ro*************@rbdev.net
Nov 14 '05 #6
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
Nov 14 '05 #7
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;
}


--
Ro*************@rbdev.net
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bangalore | last post by:
Hi, Like fundamental data types (int,char,float,...),why can't we are not able to make userdefined datatypes as extern. if "file1.C" conatains struct a { int x; } b = { 3 };
7
by: Serve Laurijssen | last post by:
How can I get the following to compile? typedef struct Y { void (*f)(X *x); } Y; typedef struct X { int x;
11
by: J Wang | last post by:
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv) { struct stat buf;
5
by: tvn007 | last post by:
I am working on the data struc. below: is there way I can abbrev. the expression below to avoid long expression. Thanks.........
9
by: Erik | last post by:
Hi, i have this struct and this linked list /* structure describing a book.*/ typedef struct { char code; char author; char title; int year; int reserved; } Book;
29
by: vishnupriya.sureshbabu | last post by:
Struct in C and C++ are same? Can Struct in C handle member functions?
6
by: sunny | last post by:
Hi All why does forward declaration does not work in following code. struct A; struct B { struct A a; // struct A *a // WORKS SINCE ITS POINTER
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
3
by: aiooua | last post by:
hello, Is the following code proper? ----- typedef int one_data; typedef long two_data; typedef struct one_{ char *name; one_data info;
2
upadhyad
by: upadhyad | last post by:
I have defined a new structure: ================== struct IDnTime { char previousEdate; struct tm theTime; struct tm *ptr; time_t t; } IDnTimeArray;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.