是的,我必须在我的编译器库中挖掘一下甚至找到它 - 我猜它是折旧的。甚至在1993年......Yeah, I had to dig a bit to even find it, in my compiler''s library -I guess it was "depreciated" even in 1993... main() 。 ...你最好做到这一点 int main(void)或者int main(int argc,char * argv []) 是的。我故意这样做是为了挑起某人回答。 < grin> 最常见的 列表中要么是#1还是#2 .......... you better make that either int main( void ) or int main( int argc, char *argv[ ] )Yup. I did that deliberately to provoke someone to answer.<grin> That''s gotta be either #1 or #2 on the ''most frequent"list... / *用(测试)名称填充数组&扩展,左对齐&空间充满(没有(字符)0')。在真正的应用程序(...'代码)中,有findfirst(), findnext(),排序,等。* / memcpy(& NameArray [0]," One 1",11); /* Fill the array with (test) names & extensions, left-justified & space-filled (no (char)0''s). In the real app (...''d code), there''s findfirst(), findnext(), sorting, etc.*/ memcpy(&NameArray[0], "One 1 ", 11); 这显然假设NameExt结构的成员是打包,即''ext''成员在 This obviously assumes that the members of the NameExt structure are packed, i.e. that the ''ext'' member starts immediately after the ''name''成员之后立即开始,其间没有任何填充。虽然您可能经常会因为编译器可以自由地在成员之间(和之后)插入尽可能多的填充字节而无法保证可以正常工作。如果它确实你的计划不再工作了。所以你更好地制作了 memcpy(& NameArray [0] .name," One" NAMLEN); memcpy(& NameArray [0]。分机,1,EXTLEN); 一个好点,正是我想知道的那种东西。''name'' member without any padding in between. While you probably often will get away with that it''s not guaranteed to work since the compiler is free to insert as many padding bytes between (and after) the members as it likes. And if it does your scheme won''t work anymore. So you better make that memcpy( &NameArray[0].name, "One ", NAMLEN ); memcpy( &NameArray[0].ext, "1 ", EXTLEN );A good point, and exactly the sort of thing I wanted to know. //构造格式字符串,如%8s%c%3s strcpy(fmtstr,"%"); strcat(fmtstr, itoa(NAMLEN,数字,10)); itoa()不是标准的C函数,因此在没有itoa()的系统上会失败。 你建议使用sprintf(),还是有一个标准的,具体的 整数到字符的函数?顺便说一下,你知道itoa() [或类似的东西]是不是标准化的,因为字符 集不是,还是有其他原因? strcat(fmtstr," s%c%"); strcat(fmtstr,itoa(EXTLEN,digits,10)); strcat(fmtstr, " s"); // Construct format string like "%8s%c%3s" strcpy(fmtstr, "%"); strcat(fmtstr, itoa(NAMLEN, digits, 10)); itoa() isn''t a standard C function, so this will fail on systems where there''s no itoa().Do you suggest sprintf(), or is there a standard, specificinteger-to-characters function? BTW, do you know if itoa()[or something equivalent] is not standardized because charactersets aren''t, or is there some other reason? strcat(fmtstr, "s%c%"); strcat(fmtstr, itoa(EXTLEN, digits, 10)); strcat(fmtstr, "s"); 为什么不用sprintf()来组成格式字符串? sprintf(fmtstr,"% %% ds %% c %%% ds" NAMLEN,EXTLEN); Why don''t you use sprintf() to make up the format string? sprintf( fmtstr, "%%%ds%%c%%%ds" NAMLEN, EXTLEN ); Argh!因为它并没有发生在我身上。我_did_考虑使用 sprintf()来格式化整个输出行,但不仅仅是 格式的字符串。Argh! Because it didn''t occur to me. I _did_ consider usingsprintf() to format the entire output line, but not just theformat string. //按照指定的格式打印 for(i = 0; i< NameCnt; i ++){ memcpy(NAM,NameArray [i] .name,NAMLEN); NAM [NAMLEN] =(char)0; "(char)0"可能更好地写成'''\0'''',至少那些具有一点经验的C程序员都会知道你的意思。 // Print according to the specified format for(i=0; i<NameCnt; i++) { memcpy(NAM, NameArray[i].name, NAMLEN); NAM[NAMLEN] = (char)0; "(char)0" is probably better written as "''\0''", at least then all C programmers with a bit of experience will know what you mean. 我_knew_有一个比(char)0更简单的方法,但是当我的 编译器接受charvar = 0x00和charvar = \ 0时,都没看过 "显式"足够多。我想我会忘记你可以在''''常数中使用转义的 字符,就像你可以在"中一样。常量。I _knew_ there was a simpler way than (char)0, but while mycompiler accepts charvar = 0x00 and charvar = \0, neither looked"explicit" enought. I guess I''d forgotten you could use escapedcharacters in '''' constants like you can in "" constants.特别是,有更简单的方法:(1)初始化NameArray记录; [您在此处重新引用的更正版本以供参考:] memcpy(& NameArray [0] .name," One" NAMLEN); memcpy(& NameArray [0] .ext," 1",EXTLEN); 也许,但这取决于你想要在实际案例中初始化数组元素。 嗯,为了便于说明,有没有更好的方法来做什么_ b $ b它_does_做什么(设置一个字符的char-array字段? /> 结构为文本常量)比使用memcpy()? 因为我不知道findfirst()和 findnext()返回(它们不是标准的C函数) 它们是MS-DOS函数;在我的库中,他们返回一个指向 a结构的指针,其中包含文件大小,日期等二进制值。 什么是标准C方式获取目录的内容?我b $ b有几本C参考书,但它们的历史可以追溯到1980年代。 (当我最初发布时,我并没有在他们身边。) Particularly, are there easier ways to: (1) initialize the NameArray records;[Your corrected version requoted here for reference:] memcpy( &NameArray[0].name, "One ", NAMLEN ); memcpy( &NameArray[0].ext, "1 ", EXTLEN ); Perhaps, but that will depend on what you want to initialize thearray elements with in the real case.Well, for illustration purposes, is there a better way to dowhat it _does_ do (setting an array-of-char field of astructure to a text constant) than using memcpy()? Since I don''t know what findfirst() and findnext() return (they aren''t standard C functions)They''re MS-DOS functions; in my library they return a pointer toa structure full of binary values for file size, dates, etc.What''s the standard-C way to get the contents of a directory? Ihave a few C reference books, but they date from the 1980''s.(And I wasn''t around them when I originally posted.)(2)指定格式字符串,假设我想坚持使用 (2) specify the format string, assuming I want to stick with 常量作为字段宽度;并且(3)printf NameArray array-of-char字段而没有明确地将它们转换为字符串?constants for the field widths; and (3) printf the NameArray array-of-char fields without explicitly converting them to strings? 你实际上不必这样做因为你指定了 You actually don''t have to do that since you specify the length of 的长度,所以即使没有终止''\ 0''字符,它也能正常工作。你可以安全地做到/或 printf(fmtstr,NameArry [0] .name,''。'',NameArray [0] .ext); 何时''fmtstr' '是例如"%787-8%C%3S" ;. 我尝试过类似的东西 - 我最初写的是%8c%c%3c, 然后意识到那不会做我的事情想要,并且 printf("%c%c%c%c%c%c%c%c%c%c%c%c", NameArray [i] .name [0], NameArray [i] .name [1], ... NameArray [i] .ext [2]); 只是在顶部。 <露齿> C没有像 FORTRAN'的隐含DO这样的东西。是吗? 我不确定%8s是安全的,''虽然我现在不记得为什么我认为它可能不是b $ b。 BTW,另一种方法是使用 printf("%* s%c%* s",NAMLEN,NameArray [0] .name,''。'', EXTLEN,NameArray [0] .ext); 或 printf("%* s%c%* s",sizeof NameArray-> name,NameArray [0] .name,''。'',sizeof NameArray- >分机,NameArray [0] .ext);them, so even without the terminating ''\0'' characters it will workcorrectly. You can safely do printf( fmtstr, NameArry[ 0 ].name, ''.'', NameArray[ 0 ].ext ); when ''fmtstr'' is e.g. "%8s%c%3s".I attempted something like that - I originally wrote "%8c%c%3c",but then realized that wouldn''t do what I wanted, andprintf("%c%c%c%c%c%c%c%c%c%c%c%c",NameArray[i].name[0],NameArray[i].name[1],...NameArray[i].ext[2]);was just "over the top". <grin> C doesn''t have anything likeFORTRAN''s "implicit DO" does it?I wasn''t sure "%8s" was safe, ''though I can''t remember now why Ithought it might not be. BTW, an alternative would be to use printf( "%*s%c%*s", NAMLEN, NameArray[ 0 ].name, ''.'', EXTLEN, NameArray[ 0 ].ext ); or printf( "%*s%c%*s", sizeof NameArray->name, NameArray[ 0 ].name,''.'', sizeof NameArray->ext, NameArray[ 0 ].ext ); 啊,%*,_这是我无法想象的。Ah, "%*", _that''s_ what I couldn''t think of.另外,有没有办法获得例如, sizeof NameExt.Ext (这是行不通的)在编译时? Also, is there a way to get e.g., sizeof NameExt.Ext (which doesn''t work) at compile-time? 是的,如果你给nameof一个真实的NameExt 结构的名称的大小,即 sizeof NameArray [ 0] .ext 或 sizeof NameArray-> ext Yes, if you give sizeof the name of a real instance of the NameExt struct, i.e. sizeof NameArray[ 0 ].ext or sizeof NameArray->ext 好的,我需要有更多的解释。为什么 sizeof(int)有效?它不是真实实例。那是什么样的paren / no paren的东西? 为什么NameArray-> ext工作,没有特定的数组索引? 我肯定对NameArray.ext感到困惑,* NameArray.ext [或 是*(NameArray).ext?]和NameArray-> ext 。你可以谅解 解释这些差异吗?我知道数组引用是(几乎?) a指针,但我不知道结构。 谢谢。 - {不要使用altavista.net回复;它已经灭绝,但我不知道如何在Google网上论坛中更改它。使用comcast而不是 " altavista"如果你想给我发电子邮件}OK, I need to have that explained a bit more. Why doessizeof(int) work? It''s not a "real instance". Is that thesizeof paren/no paren thing?Why does NameArray->ext work, without a specific array index?And I''m definitely confused by NameArray.ext, *NameArray.ext [oris that *(NameArray).ext?], and NameArray->ext. Could you kindlyexplain the differences? I know an array reference is (almost?)a pointer, but I don''t know for structs.Thanks.--{ Don''t use the "altavista.net" reply-to; it''s extinct, but I don''tknow how to change it in Google Groups. Use "comcast" instead of"altavista" if you want to email me. } gtippery< gt ****** @ altavista.net>写道:gtippery <gt******@altavista.net> wrote: #include< stdlib.h> / * for itoa * / 很抱歉,但itoa()是一个你不会总是在< stdlib.h>中找到的扩展名。 是的,我不得不在我的编译器库中挖掘一下甚至找到它 - 我想它是折旧的。即使在1993年... 它并没有被弃用,因为它从未成为标准要求的功能集的一部分 。 #include <stdlib.h> /* for itoa */ Sorry, but itoa() is an extension that you won''t always find in <stdlib.h>. Yeah, I had to dig a bit to even find it, in my compiler''s library - I guess it was "depreciated" even in 1993...It wasn''t deprecated because it never was part of the set of functionsthe standard requires. main() ... ...你最好把它做成 int main( void)或者 int main(int argc,char * argv []) you better make that either int main( void ) or int main( int argc, char *argv[ ] ) 是的。我故意这样做是为了挑起某人回答。< grin>在''最常见的'列表中,它必须是#1或#2 ...... Yup. I did that deliberately to provoke someone to answer. <grin> That''s gotta be either #1 or #2 on the ''most frequent" list... 你好像不是对clc有太好的看法;-)You don''t seem to have too good an opinion about clc;-) itoa()不是标准的C函数,因此在系统上会失败那里没有itoa()。 您是否建议使用sprintf(),或者是否存在标准的,特定的整数到字符函数?顺便说一句,你知道itoa() [或类似的东西]是不是标准化的,因为角色没有设置,或者还有其他原因吗? 我不知道itoa()会有什么用处,因为它只会复制你已经拥有的sprintf()功能。我猜有人为了对称性而发明了它,因为有atoi(),但是atoi() 无论如何都是一个相当无用的函数,应该避免使用 strtol()。 itoa() isn''t a standard C function, so this will fail on systems where there''s no itoa(). Do you suggest sprintf(), or is there a standard, specific integer-to-characters function? BTW, do you know if itoa() [or something equivalent] is not standardized because character sets aren''t, or is there some other reason?I don''t know what itoa() would be good for since it only reproducesa functionality you already have with sprintf(). I guess someoneinvented it for symmetry reasons because there''s atoi(), But atoi()is a rather useless function anyway and should be avoided in favourof strtol().特别是,有更简单的方法:(1)初始化NameArray记录; [您在此处重新引用的更正版本以供参考:] Particularly, are there easier ways to: (1) initialize the NameArray records; [Your corrected version requoted here for reference:] memcpy(& NameArray [0] .name," One" ,NAMLEN); memcpy(& NameArray [0] .ext," 1",EXTLEN); 也许,但这取决于你想要在实际情况下初始化数组元素。 好吧,为了便于说明,有没有比使用memcpy更好的方法来做什么(将_ />结构的字符数组字段设置为文本常量)? )? 我在这里看不到更好的方式。 因为我不知道findfirst()和 findnext() return(它们不是标准的C函数) memcpy( &NameArray[0].name, "One ", NAMLEN ); memcpy( &NameArray[0].ext, "1 ", EXTLEN ); Perhaps, but that will depend on what you want to initialize the array elements with in the real case. Well, for illustration purposes, is there a better way to do what it _does_ do (setting an array-of-char field of a structure to a text constant) than using memcpy()?I don''t see any better way here. Since I don''t know what findfirst() and findnext() return (they aren''t standard C functions) 它们是MS-DOS函数;在我的库中,它们返回一个指向一个结构,其中包含文件大小,日期等二进制值。获取目录内容的标准C方法是什么?我有一些C参考书,但它们可以追溯到20世纪80年代。(当我最初发布时,我并没有在他们周围。) 没有任何标准的C函数,你必须为你的操作系统使用扩展名 - 单词directory和甚至没有出现在任何地方 C标准。 另外,有没有办法获得例如, sizeof NameExt.Ext (在编译时不起作用)? They''re MS-DOS functions; in my library they return a pointer to a structure full of binary values for file size, dates, etc. What''s the standard-C way to get the contents of a directory? I have a few C reference books, but they date from the 1980''s. (And I wasn''t around them when I originally posted.)There aren''t any standard C functions, you must use the extensionsfor your OS - the word "directory" doesn''t even appear anywhere inthe C standard. Also, is there a way to get e.g., sizeof NameExt.Ext (which doesn''t work) at compile-time? 是的,如果你给nameof一个真实的NameExt 结构名称的大小,即 sizeof NameArray [0] .ext 或 sizeof NameArray-> ext Yes, if you give sizeof the name of a real instance of the NameExt struct, i.e. sizeof NameArray[ 0 ].ext or sizeof NameArray->ext 好的,我需要那个解释了一下。为什么 sizeof(int)有效?它不是真实实例。这是paren / no paren的大小吗? 是的,确实如此。如果没有括号,您只能使用 变量的名称。如果你想使用类型,它们必须用 括号括起来。 为什么NameArray-> ext工作,没有特定的数组索引? 因为NameArray在这里被自动作为指向 的指针,是NameArray数组的第一个元素(比如当你传递 $ b时) $ b数组到函数)。 我肯定对NameArray.ext感到困惑,* NameArray.ext [或是*(NameArray).ext?],以及NameArray->分机 如果你有一系列的结构,比如 struct xyz { int x; char y; } struct xyz a [10]; then'' a'''衰变在它被用作指向数组第一个元素的 指针的值的情况下。因此'' - > y''与''[0] .y''相同 (就像''(* a).y'')。 我知道一个数组引用(几乎是?)一个指针,但我不知道结构。 OK, I need to have that explained a bit more. Why does sizeof(int) work? It''s not a "real instance". Is that the sizeof paren/no paren thing?Yes, it is. Without the parentheses you can only use names ofvariables. If you want to use types they must be enclosed inparenthesis. Why does NameArray->ext work, without a specific array index?Because NameArray is automatically taken here to be a pointer tothe first element of the NameArray array (like when you pass anarray to a function). And I''m definitely confused by NameArray.ext, *NameArray.ext [or is that *(NameArray).ext?], and NameArray->ext.When you have an array of structures likestruct xyz {int x;char y;}struct xyz a[ 10 ];then ''a'' "decays" in situations where it''s used as a value into apointer to the first element of the array. Thus ''a->y'' is the sameas ''a[0].y'' (as is ''(*a).y''). I know an array reference is (almost?) a pointer, but I don''t know for structs. 它没有没关系,如果你有一系列的整数或结构或工会 或函数指针或其他什么,它总是一样的。当一个数组的名称 被用于它被使用的情况时,好像有一个值 (比如''a-> y'' )然后它会自动转换为指向 指针的数组的第一个元素,无论 数组有哪种元素。另请参阅Chris Torek所说的规则: http://web.torek.net/torek/c/index.html 问候,Jens - \ Jens Thoms Toerring ___ Je ****** *****@physik.fu-berlin.de \ __________________________ http://www.toerring.deIt doesn''t matter if you have an array of ints or structures or unionsor function pointers or whatever, it''s always the same. When the nameof an array is used in a situation where it''s used as if had a value(like in ''a->y'') then it automatically is converted to a pointer tothe first element of the array, no matter what kind of elements thearray has. See also what Chris Torek calls "The Rule": http://web.torek.net/torek/c/index.htmlRegards, Jens--\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de\__________________________ http://www.toerring.de 这篇关于请帮助优化(和标准化)此代码...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 11:01