📄 symtab.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:47 2005 by faqproc version 2.7 --><!-- from source file misctechn.sgml dated Sat Sep 28 22:23:36 2002 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/misc/symtab.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:04 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 20.6</title><link href="binaryfiles.html" rev=precedes><link href="intovf.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="binaryfiles.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="intovf.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 20.6</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>If I have a <TT>char *</TT> variablepointing tothe name of a function,how can I call that function?Code like<pre> extern int func(int, int); char *funcname = "func"; int r = (*funcname)(1, 2);</pre>or<pre> r = (*(int (*)(int, int))funcname)(1, 2);</pre>doesn't seem to work.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>By the time a program is running, information aboutthe names of its functions and variables(the``symbol table'') is no longerneeded,and may therefore not beavailable.The most straightforward thing to do,therefore,isto maintainthat information yourself,witha correspondencetable of names and function pointers:<pre>int one_func(), two_func();int red_func(), blue_func();struct { char *name; int (*funcptr)(); } symtab[] = { "one_func", one_func, "two_func", two_func, "red_func", red_func, "blue_func", blue_func,};</pre>Then,search the table for the name, and callviatheassociated function pointer,with code like this:<pre>#include <stddef.h>int (*findfunc(char *name))(){ int i; for(i = 0; i < sizeof(symtab) / sizeof(symtab[0]); i++) { if(strcmp(name, symtab[i].name) == 0) return symtab[i].funcptr; } return NULL;}... char *funcname = "one_func"; int (*funcp)() = findfunc(funcname); if(funcp != NULL) (*funcp)();</pre>The callablefunctions should all havecompatible argument and return types.(Ideally,the function pointers would also specify the argument types.)</p><p>It issometimes possiblefor a program to read its own symbol tableif it is still present,but it must first be able to find its own executable(see question<a href="../osdep/exepath.html">19.31</a>),and it must know how to interpret the symbol table(someUnixC libraries providean <TT>nlist</TT> function for this purpose).See also questions<a href="../struct/fieldnames.html">2.15</a>,<a href="../resources/expreval.html">18.14</a>,and<a href="../osdep/dynlink.html">19.36</a>.</p><p>References:PCS Sec. 11 p. 168<br></p><!-- aend --><p><hr><a href="binaryfiles.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="intovf.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/misc/symtab.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:04 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -