Mail Archives: geda-help/2012/01/23/18:53:49
Andrew Poelstra:
> On Mon, Jan 23, 2012 at 12:13:55PM -0800, Colin D Bennett wrote:
> > On Sun, 22 Jan 2012 15:43:06 -0800
> > Andrew Poelstra <asp11 AT sfu DOT ca> wrote:
> >
> > > Function pointers may not be casted to function pointers of
> > > different types. (This is very irritating, but it's what the
> > > standard says.)
> >
> > I don't see how it's irritating. It's just the only sane thing to
> > do. How is a function pointer usefull unless you know the argument
> > types?
> >
>
> Because a lot of times you have unnecessary arguments -- i.e., suppose
> you had an object that looked like
>
> typedef struct stream_t {
> /* some public data */
> int (*open) (struct stream_t *, int mode);
> /* some other functions */
> } stream_t;
...
If you want variable length argument list, why don't you use a variable
length argument list?
$ cat a.c
#include <stdarg.h>
#include <stdio.h>
typedef struct stream_t {
/* some public data */
int (*open) (struct stream_t *, ...);
/* some other functions */
} stream_t;
int f(struct stream_t *this, ...);
int g(struct stream_t *that, ...);
int main(void) {
stream_t a;
a.open = f;
a.open = g;
f(&a);
g(&a, 10);
return 0;
}
int f(struct stream_t *this, ...) {
printf("hello f\n");
return 0;
}
int g(struct stream_t *that, ...) {
va_list ap;
int mode;
va_start(ap, that);
mode = va_arg(ap, int);
va_start(ap, that);
printf("mode %d\n", mode);
return 0;
}
$ gcc a.c
$ ./a.out
hello f
mode 10
$
///
Since you pass a struct to the open(), why don't you just put the
"int mode" into the struct:
typedef struct stream_t {
/* some public data */
int (*open) (struct stream_t *);
int mode;
/* some other functions */
} stream_t;
then the open who needs it can get it from the struct.
Regards,
/Karl Hammar
-----------------------------------------------------------------------
Aspö Data
Lilla Aspö 148
S-742 94 Östhammar
Sweden
+46 173 140 57
- Raw text -