本文介绍了Pascal - &gt; C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Hallo allemaal, 在我的Pascal程序中,我在一个函数中收集了所有消息。所有 消息都有自己的号码。这种方法的优点之一 是如果有人想要更改消息,他只需要在一个地方查看 。 该函数声明如下: 函数zin(by:byte):string; 调用函数的行看起来像简单如下 这个: writeln(zin(12)); 到目前为止,我找到了这个函数的替代品但我不满足于b 。我定义了一个''char pst [255]''和函数''void zin(int by)''填充pst和想要的消息。但这意味着我现在 需要两行而不是一行: zin(12); printf("%s",pst ); 当然我试图把''zin(int by)''变成一个函数, 返回一个值,所以我可以把它们结合起来两行就像 ''printf("%s",zin(12));''但是我得到了编译错误或只有 废话出现了在屏幕上。 同时我找到了一个解决方案,形式为 char * zin [20] = {" ..", "" ..."等等。$ printf("%s",zin [12]); 但这适用于固定邮件但是那些邮件 是由动态函数生成的吗? 欢迎任何帮助! - ___ / __ | __ / / | _ / Groetjes,Ruud Baltissen \ \__ | _ \ \ ___ | http://Ruud.C64.org Hallo allemaal,In my Pascal program I gathered all messages in one function. Allmessages have their own number. One of the advantages of this methodis that if someone wants to change a messages, he only has to look inone place. The function is declared as follow:function zin(by : byte) : string; A line calling the function can look like as simple asthis:writeln(zin(12)); So far I have found a replacement for this function but I''m notsatisfied with it. I defined a ''char pst[255]'' and the function ''voidzin(int by)'' fills pst with the wanted message. But that means I nowneed two lines instead of one:zin(12);printf("%s", pst); Of course I tried to turn ''void zin(int by)'' into a funtion thatreturns a value so I could combine those two line to something like''printf("%s", zin(12));'' but I either got an compilation error or onlynonsense appeared on the screen. In the meanwhile I found a solution in the form ofchar *zin[20] = { "..", ""..." etc. }printf("%s", zin[12]); But that works fine for fixed messages but what about messages thatare generated by a function on the fly? Any help is welcome!--___/ __|__/ / |_/ Groetjes, Ruud Baltissen\ \__|_\\___| http://Ruud.C64.org推荐答案 为什么不修改zin返回(指向第一个字符的指针) 字符串? const char * zin(int msg) { static char pst [MAX_LENGTH]; / *在您方便时定义MAX_LENGTH * / const char * msglist [] = { " Hello", Goodbye, Whatever }; size_t count = sizeof msglist / sizeof msglist [0]; if(msg> = 0&& msg< count) { strcpy(pst, msglist [msg]); } 其他 { / *大概这是你的机器 - 生成的消息是 生成,并加载到pst * / } 返回pst ; } 现在printf("%s",zin(12))变得合法 - 但要注意!静态缓冲区 表示您只有一个存储区域用于当前消息,因此 printf("%s%s",zin) (12),zin(1))很可能不会做你想要的东西 来做。如果这是一个问题,你需要考虑将缓冲区传递给 函数。 - Richard Heathfield< http://www.cpax.org.uk> 电子邮件:-http:// www。 + rjh @ 谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php> Usenet是一个奇怪的放置" - dmr 1999年7月29日 Why not modify zin to return (a pointer to the first character of) thestring? const char *zin(int msg){static char pst[MAX_LENGTH]; /* define MAX_LENGTH at your convenience */ const char *msglist[] ={"Hello","Goodbye","Whatever"};size_t count = sizeof msglist / sizeof msglist[0];if(msg >= 0 && msg < count){strcpy(pst, msglist[msg]);}else{/* presumably this is where your machine-generated messages aregenerated, and loaded into pst*/} return pst;} Now printf("%s", zin(12)) becomes legal - but beware! The static buffermeans that you''ve only got one storage area for "the current message", soprintf("%s %s", zin(12), zin(1)) will very likely not do what you want itto do. If that''s a problem, you need to think about passing a buffer intothe function. --Richard Heathfield <http://www.cpax.org.uk>Email: -http://www. +rjh@Google users: <http://www.cpax.org.uk/prg/writings/googly.php>"Usenet is a strange place" - dmr 29 July 1999 如果你想知道什么是消息,它永远不会出现在你看的 的地方...... /> 非常长的宏名称可以提供帮助。 错误(27); 和某处 {27,通信链接丢失} 因此更好的宏可能是 错误(LINK_LOST); 我参与了一个错误宏看起来像这样的项目 错误(SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST); 但是这个是国际化(I18Ning)实际输出 字符串。 and if you want to know what a message is it''s never in theplace where you look... Very long macro names can help. error (27); and somewhere is{27, "communications link is lost"} hence a better macro might beerror (LINK_LOST); I worked on a project where the error macros looked like thiserror (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST); But this was internationalising (I18Ning) the actual outputstrings. 你可以返回静态字符串 const char * zin(int n) { 开关(n) { 案例1:返回链接丢失; 休息; 案例2:返回O2超压; 休息; 默认: 返回坏错误代码; } } printf("%s",zin(12)); you could return static strings const char* zin (int n){switch (n){case 1: return "link lost";break;case 2: return "O2 over pressure";break;default:return "bad error code";}} printf ("%s", zin(12)); 显示你的代码 show your code 是的。或者 struct String_tab_entry { int n; const char * str; }; String_tab_entry string_tab [] = { {12," tile lost" }, {99," intruder"}, (101,fire}, }; 然后搜索数组。字符串然后不必连续 甚至订购。(订购可以使搜索更快) yep. Or struct String_tab_entry{int n;const char *str;}; String_tab_entry string_tab [] ={{12, "tile lost"},{99, "intruder"},(101, "fire"},}; Then search the array. The strings then don''t have to be contiguousor even ordered. (tho'' ordered can make the search faster) 在这种情况下你需要某种缓冲。这是相当的标准C问题。解决方案是 1。使用静态缓冲区 const char * zin(int n,const char * arg) { static char buffer [ 256]; 开关(n) { 案例1: sprintf(缓冲区,链接丢失%s,arg); 返回缓冲区; } } 多次通话可能有问题 printf("%s%s",zin(12),zin(13)); 2.传递一个缓冲区 const char * zin(int n,char * buffer,const char * arg) {switch(n) { 案例1: sprintf(缓冲区,链接丢失%s,arg); 返回缓冲区; } } 3. malloc缓冲区(但有人必须清理 之后)。 4.让zin()更加聪明(未经测试的代码!) #include< stdarg.h> typedef void PrintFunc(const char *); void zin(int n,PrintFunc * print,const char * format,...) { va_list args; va_start(args,format); char buffer [256]; vsprintf(buffer,format,args); prin t(缓冲区); } void print(const char * s) { printf("%s",s); } zin(23,print,link lost%d,link_id); 这有点不整洁。但关键是你可以使用stdarg功能将仲裁 参数传递给zin()。你可以通过函数参数来实现实际的i / o. 如果不了解你的应用程序的更多信息,我就不能再进一步了。 b $ b希望我已经给了你一些想法。 - Nick Keighley In that case you need a buffer of some sort. This is fairlystandard C problem. Solutions are 1. use a static buffer const char* zin (int n, const char* arg){static char buffer [256]; switch (n){case 1:sprintf (buffer, "link lost %s", arg);return buffer;}} Multiple calls may be a problem printf ("%s %s", zin(12), zin(13)); 2. pass a buffer const char* zin (int n, char *buffer, const char* arg){ switch (n){case 1:sprintf (buffer, "link lost %s", arg);return buffer;}} 3. malloc a buffer (but then someone has to clean upafterwards). 4. Make zin() a lot cleverer (Untested code!) #include <stdarg.h> typedef void PrintFunc (const char*); void zin (int n, PrintFunc *print, const char *format, ...){va_list args;va_start (args, format);char buffer [256];vsprintf (buffer, format, args);print (buffer);} void print (const char *s){printf ("%s", s);} zin (23, print, "link lost %d", link_id);This is a bit untidy. But the point is you can pass arbitaryarguments to zin() using stdarg functionality. You canalso pass function arguments to do the actual i/o. Without knowing more about your app I can''t go any furthur.Hope I''ve given you some ideas.--Nick Keighley 如果你想知道什么是消息,它就永远不会出现在你看的 的地方...... /> 非常长的宏名称可以提供帮助。 错误(27); 和某处 {27,通信链接丢失} 因此更好的宏可能是 错误(LINK_LOST); 我参与了一个错误宏看起来像这样的项目 错误(SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST); and if you want to know what a message is it''s never in theplace where you look...Very long macro names can help. error (27);and somewhere is {27, "communications link is lost"}hence a better macro might be error (LINK_LOST);I worked on a project where the error macros looked like this error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST); 或者在这种情况下,甚至更好,使用枚举。 Richard Or in this case, even better, use an enum. Richard 这篇关于Pascal - &gt; C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-18 21:40