Structures and Variable Functions

K

kelvSYC

Can variable functions be implemented in C, and if so, how?

Also, can you have a function or a function prototype as a member in a
structure?
 
B

Ben Pfaff

kelvSYC said:
Can variable functions be implemented in C, and if so, how?

Please define "variable function".
Also, can you have a function or a function prototype as a member in a
structure?

No, but you may have a pointer to a function.
 
R

Richard Heathfield

kelvSYC said:
Can variable functions be implemented in C, and if so, how?

I checked the index in Maclennan, and the index in the Dragon book, for
"variable function". No dice. What do you mean by it?
Also, can you have a function or a function prototype as a member in a
structure?

You can have a function pointer as a structure member.
 
D

Derk Gwen

# Can variable functions be implemented in C, and if so, how?

Yes.

# Also, can you have a function or a function prototype as a member in a
# structure?

Yes.

(cd /tmp
cat <<':eof' >t.c
#include <stdio.h>
#include <stdlib.h>
typedef int (*func)(char *s);
typedef struct {func f; char *s;} object;

int X(char *s) {printf("X-%s-X\n",s); return 1;}
int Y(char *s) {printf("Y-%s-Y\n",s); return 2;}

object choose(char *s) {
object o; o.s = s;
switch (*s) {
case 'x': o.f = X; break;
case 'y': o.f = Y; break;
}
return o;
}
int apply(object o) {return o.f(o.s);}

int main(int N,char **P) {
int r = apply(choose(P[1]));
printf("return %d\n");
return 0;
}
:eof
cc t.c
a.out xyzzy)

X-xyzzy-X
return 0
 
J

Jack Klein

Can variable functions be implemented in C, and if so, how?

What do you mean by a variable function? If you mean a function like
printf(), that accepts a variable number and type of arguments, the
standard header said:
Also, can you have a function or a function prototype as a member in a
structure?

You can have a pointer to a function as a member of a structure.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
P

pete

kelvSYC said:
Can variable functions be implemented in C, and if so, how?

I don't know what a variable function is.
Also, can you have a function or a function prototype as a member in a
structure?

You can have a pointer to a function, as a member in a structure.
 
D

Douglas A. Gwyn

kelvSYC said:
Can variable functions be implemented in C, and if so, how?

What specifically do you mean by "variable function"?
Also, can you have a function or a function prototype as a member in a
structure?

Functions live in a different address space than data, so no.
Quite probably you should consider using a *pointer* to a
function; the pointer lives in data space.
 
U

Ulrich Eckhardt

kelvSYC said:
Can variable functions be implemented in C, and if so, how?

Which is exactly what ? Not being sure what you mean, I suggest taking a
look at the way varargs work (e.g. in printf()). Another thing to look at
are function-pointer.
Also, can you have a function or a function prototype as a member in a
structure?

No. What for, anyways ? If you want a _member_function, use C++.

Uli
 
F

Francis Glassborow

kelvSYC said:
Can variable functions be implemented in C, and if so, how?

Also, can you have a function or a function prototype as a member in a
structure?

Not in C, though you could have a function pointer if that is any help
to you.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
Can variable functions be implemented in C, and if so, how?

What the hell is a 'variable function'? Maybe you want a pointer to a
function? Or are you talking about class function member? In this case, you
want C++ instead of C.
Also, can you have a function or a function prototype as a member in a
structure?

No. All you can have is a pointer to a function. Once correctly initialized,
it is callable.
 
H

Hans-Bernhard Broeker

In comp.lang.c.moderated kelvSYC said:
Can variable functions be implemented in C, and if so, how?

Close to impossible to tell until you specify what exactly you mean by
a "variable function".
Also, can you have a function or a function prototype as a member in a
structure?

No. But function pointers can.
 
K

kelvSYC

Can variable functions be implemented in C, and if so, how?
Please define "variable function".

Using the value of a string to call a function with the same name. For
example, a way of calling a function named foo() using a string named
"foo". Then, if you change "foo" to "bar", the same code would call
bar().
 
R

Richard Heathfield

kelvSYC said:
Using the value of a string to call a function with the same name. For
example, a way of calling a function named foo() using a string named
"foo". Then, if you change "foo" to "bar", the same code would call
bar().

FAQ 20.6 - see my sig block for FAQ URL.
 
J

John Bode

kelvSYC said:
Using the value of a string to call a function with the same name. For
example, a way of calling a function named foo() using a string named
"foo". Then, if you change "foo" to "bar", the same code would call
bar().

Short answer: no.

Long answer: not directly. You'd have to implement a lookup table
that contains both the string and the function pointer. You'd search
the table for the string, then call the function using the associated
pointer.

Example:

/*
** Assuming all the functions you call return void, and
** take no arguments; simplest case
*/
typedef void (*fptr)(void);

/*
** Lookup table associating function name string
** with function pointer:
*/
struct flookup {
char fname[FNAMELEN];
fptr func;
};

void foo (void) {...}
void bar (void) {...}

struct flookup lkuptable[NFUNCS] = {"foo", foo, "bar", bar, ...};

void CallByName (char *fname)
{
int i = 0;

for (i = 0; i < NFUNCS; i++)
{
if (!strcmp (fname, lkuptable.fname))
{
lkuptable.func ();
break;
}
}
}

Things get a bit trickier if you have to support functions with
different prototypes. You'd have to use a union or something to
handle the different function types, and you'd have to find a way to
pass the necessary arguments to the CallByName function. In all, the
bookkeeping gets to be more trouble than it's worth.
 
T

Thomas Matthews

kelvSYC said:
Using the value of a string to call a function with the same name. For
example, a way of calling a function named foo() using a string named
"foo". Then, if you change "foo" to "bar", the same code would call
bar().

You have two choices: cascading "if-else" ladder and a table.
You have to associate a function pointer with a string.

void Execute_Function(const char * const name)
{
if (strcmp(name, "foo") == 0)
Foo();
else if (strcmp(name, "bar") == 0)
Bar();
// Und so weite.
}

Or:
typedef void (*FUNC_PTR)(void);

struct Func_Record
{
const char * name;
FUNC_PTR function;
};

struct Func_Record func_table[] =
{
{"foo", Foo}, {"bar", Bar}
};
const unsigned int NUM_FUNCS = sizeof(func_table)
/ sizeof(func_table[0]);

void Execute_Function(const char * const name)
{
unsigned int i;
for (i = 0; i < NUM_FUNCS; ++i)
{
if (strcmp(func_table.name, name) == 0)
{
func_table.function();
break;
}
}
return;
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

John Potter

Also, can you have a function or a function prototype as a member in a
structure?

Just adding some amusement to your question.

typedef int Func (void); /* What is this? */
int g (Func func) { /* It is a pointer to a function */
return func();
}
int f () {
return 42;
}
struct S {
Func f1; /* It is a function. Ok C++ but not C */
Func* f2; /* This is a pointer to a function */
};
int main () {
g(f); /* valid */
g(&f); /* valid */
}

Did this have anything to do with your question?

John
 
K

kelvSYC

Also, can you have a function or a function prototype as a member in a
You can have a pointer to a function as a member of a structure.

Can you please explain how that would work (as none of my references
mention this sort of thing)?
 
L

LibraryUser

Douglas A. Gwyn said:
What specifically do you mean by "variable function"?


Functions live in a different address space than data, so no.
Quite probably you should consider using a *pointer* to a
function; the pointer lives in data space.

I think you may want to reconsider that statement. On some
machines it may be so, but it is not part of the standard, and
the namespace is the same. However the only actions that may be
performed on a function is to call it or to form a pointer to it,
which can in turn be used to call it. Which is equally unclear.
 
B

Barry Schwarz

Can you please explain how that would work (as none of my references
mention this sort of thing)?

Having a structure with a pointer to a function as a member is
conceptually identical to having a structure with a pointer to object
as a member.

struct x {
int i;
double d;
int *pi;
struct x *px;
double (*pf)(double, double);
} my_x;

my_x.pf = pow;


<<Remove the del for email>>
 
D

Douglas A. Gwyn

LibraryUser said:
I think you may want to reconsider that statement.

No. The model I presented is the one that makes it
easiest to understand how to progam such things in a
portable manner. While separate I/D space is not
required, it is possible, so that more restrictive
possibility is the one to use as a guideline.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top