Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structures and Variable Functions

Reply
Thread Tools

Structures and Variable Functions

 
 
kelvSYC
Guest
Posts: n/a
 
      09-14-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-14-2003
kelvSYC <(E-Mail Removed)> 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;}
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-14-2003
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 : (E-Mail Removed)
"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: (E-Mail Removed)
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      09-14-2003
kelvSYC <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-14-2003
On 14 Sep 2003 01:28:31 GMT, kelvSYC <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-14-2003
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: (E-Mail Removed)
 
Reply With Quote
 
Douglas A. Gwyn
Guest
Posts: n/a
 
      09-14-2003
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: (E-Mail Removed)
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      09-14-2003
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: (E-Mail Removed)
 
Reply With Quote
 
Francis Glassborow
Guest
Posts: n/a
 
      09-14-2003
In article <(E-Mail Removed)>, kelvSYC
<(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-14-2003
In 'comp.lang.c', kelvSYC <(E-Mail Removed)> 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- (E-Mail Removed) [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: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
structures and functions N Yiannakoulias C Programming 5 09-19-2003 06:05 PM



Advertisments