473,396 Members | 2,002 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,396 software developers and data experts.

Structures and Variable Functions

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?

--
I am only a mirage.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #1
19 2781
kelvSYC <ke*****@no.email.shaw.ca> writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #2
kelvSYC wrote:
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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #3
kelvSYC <ke*****@no.email.shaw.ca> wrote:
# 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

--
Derk Gwen http://derkgwen.250free.com/html/index.html
But I do believe in this.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #4
On 14 Sep 2003 01:28:31 GMT, kelvSYC <ke*****@no.email.shaw.ca> wrote
in comp.lang.c:
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 <stdarg.h> provides macros for writing such functions.
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #5
kelvSYC wrote:

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.

--
pete
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #6
kelvSYC wrote:
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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #7
kelvSYC wrote:
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

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #8
In article <cl****************@plethora.net>, kelvSYC
<ke*****@no.email.shaw.ca> writes
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.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #9
In 'comp.lang.c', kelvSYC <ke*****@no.email.shaw.ca> wrote:
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.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #10
In comp.lang.c.moderated kelvSYC <ke*****@no.email.shaw.ca> wrote:
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.
--
Hans-Bernhard Broeker (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #11
> > 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().

--
I am only a mirage.
Nov 13 '05 #12
kelvSYC wrote:
> 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().


FAQ 20.6 - see my sig block for FAQ URL.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #13
kelvSYC <ke*****@no.email.shaw.ca> wrote in message news:<140920031912546655%ke*****@no.email.shaw.ca> ...
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().


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[i].fname))
{
lkuptable[i].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.
Nov 13 '05 #14
kelvSYC wrote:
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().


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[i].name, name) == 0)
{
func_table[i].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

Nov 13 '05 #15
On 14 Sep 2003 01:28:31 GMT, kelvSYC <ke*****@no.email.shaw.ca> wrote:
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #16
> > 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.


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

--
I am only a mirage.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #17
"Douglas A. Gwyn" wrote:
kelvSYC wrote:
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.


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.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #18
On 16 Sep 2003 07:14:56 GMT, kelvSYC <ke*****@no.email.shaw.ca> wrote:
> 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.


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>>
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #19
LibraryUser wrote:
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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #20

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

Similar topics

2
by: Alberto Vera | last post by:
Hello: I have next structures: A AB ABC ABCD ABCDE ABCDEF
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
12
by: addinall | last post by:
Hi guys and gals, Haven't been in here for a good decade or so! I'm having a braid-dead moment that is lasting for a couple of days. Declaring pointers to functions witrhin structures and...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any corrections? ...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.