请帮助优化(和标准化)此代码... [英] Please help optimize (and standarize) this code...

查看:58
本文介绍了请帮助优化(和标准化)此代码...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题 - 我已经离开了C长达一段时间了。


在我看来,应该有更简单(或至少更短)的方式

来做这件事。它编译和编译为我而奔跑(使用PowerC,一个

16位DOS编译器);如果有非标准或意外作品

方面,请告诉我。


{如果我知道*这就是这种情况什么*谷歌,我

不需要* ... ...< grin>}


代码如下:


---------------------- snip ------------------- -----

#include< stdio.h>

#include< stdlib.h> / * for itoa * /

#include< string.h> / *表示memcpy(),strcpy(),strcat()* /


#define NAMLEN 8

#define EXTLEN 3

#define MAX 512

#define MAXCOL 1

typedef struct {

char name [NAMLEN];

char ext [EXTLEN];

} NameExt;


NameExt NameArray [MAX + 1];

int NameCnt = 0;


main()

{

// ...

char fmtstr [32],digits [12];

int i;

char NAM [NAMLEN + 1],EXT [EXTLEN + 1];

int colnum = 0;


// ...

/ *用(测试)名称填充数组&扩展,

左对齐&空间充满(没有(字符)0')。

在真正的应用程序(...'代码)中,有找到第一个(),

findnext(),排序等* /


memcpy(& NameArray [0]," One 1",11);

memcpy(& NameArray [1]," TwoTwoTw222",11);

memcpy(& NameArray [2]," 333 003",11);

NameCnt = 3;

// ...


/ *打印名称&多列中的扩展名* /


//构造格式字符串,如%8s%c%3s

strcpy(fmtstr,"%" );

strcat(fmtstr,itoa(NAMLEN,digits,10));

strcat(fmtstr," s%c%");

strcat(fmtstr,itoa(EXTLEN,digits,10));

strcat(fmtstr," s");


//按照指定的格式打印

for(i = 0; i< NameCnt; i ++){

memcpy(NAM,NameArray [i] .name,NAMLEN); < br $>
NAM [NAMLEN] =(char)0;

memcpy(EXT,NameArray [i] .ext,EXTLEN);

EXT [EXTLEN ] =(char)0;

printf(fmtstr,NAM,''。'',EXT);


//在此处理列化

if(colnum ++< MAXCOL){

printf("");

} else {

printf( " \ n");

colnum = 0;

}

}

printf(" \ n");

// ...

}

-------------- --------剪辑 - ----------------------


特别是,有更简单的方法:

(1)初始化NameArray记录;

(2)指定格式字符串,假设我想坚持使用常量

作为字段宽度;并且

(3)printf NameArray array-of-char字段没有明确

将它们转换为字符串?


此外,有没有办法在编译时获得例如,
sizeof NameExt.Ext

(哪些不起作用)?


TIA!

Newbie-ish questions - I''ve been away from C for a _long_ time.

It seems to me that there ought to be easier (or at least shorter) ways
to do what this does. It does compile & run for me (with PowerC, a
16-bit DOS compiler); if there are nonstandard or "accidentally-works"
aspects, please let me know.

{This is the sort of situation where if I knew *what* to Google for, I
wouldn''t *need* to... <grin>}

Code follows:

---------------------- snip ------------------------
#include <stdio.h>
#include <stdlib.h> /* for itoa */
#include <string.h> /* for memcpy(), strcpy(), strcat() */

#define NAMLEN 8
#define EXTLEN 3
#define MAX 512
#define MAXCOL 1

typedef struct {
char name[NAMLEN];
char ext[EXTLEN];
} NameExt;

NameExt NameArray[MAX+1];
int NameCnt=0;

main()
{
//...
char fmtstr[32], digits[12];
int i;
char NAM[NAMLEN+1], EXT[EXTLEN+1];
int colnum = 0;

//...
/* 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);
memcpy(&NameArray[1], "TwoTwoTw222", 11);
memcpy(&NameArray[2], "333 003", 11);
NameCnt = 3;
//...

/* Print names & extensions in multiple columns */

// Construct format string like "%8s%c%3s"
strcpy(fmtstr, "%");
strcat(fmtstr, itoa(NAMLEN, digits, 10));
strcat(fmtstr, "s%c%");
strcat(fmtstr, itoa(EXTLEN, digits, 10));
strcat(fmtstr, "s");

// Print according to the specified format
for(i=0; i<NameCnt; i++) {
memcpy(NAM, NameArray[i].name, NAMLEN);
NAM[NAMLEN] = (char)0;
memcpy(EXT, NameArray[i].ext, EXTLEN);
EXT[EXTLEN] = (char)0;
printf(fmtstr, NAM, ''.'', EXT);

// Handle columnizing here
if(colnum++ < MAXCOL) {
printf(" ");
} else {
printf("\n");
colnum = 0;
}
}
printf("\n");
//...
}
---------------------- snip ------------------------

Particularly, are there easier ways to:
(1) initialize the NameArray records;
(2) specify the format string, assuming I want to stick with constants
for the field widths; and
(3) printf the NameArray array-of-char fields without explicitly
converting them to strings?

Also, is there a way to get e.g.,
sizeof NameExt.Ext
(which doesn''t work) at compile-time?

TIA!

推荐答案

gtippery< gt ****** @ altavista.net>写道:
gtippery <gt******@altavista.net> wrote:
新手问题 - 我已经离开C了很长时间了。
在我看来,应该有更容易(或至少更短)的方式来做这件事。它编译和编译为我跑(用PowerC,一个16位DOS编译器);如果有非标准或意外工作的方面,请告诉我。
{这种情况如果我知道*对Google来说是什么*,我就不需要*来......< grin>}
代码如下:
---------------------- snip ----------------------- -
#include< stdio.h>
#include< stdlib.h> / * for itoa * /


很抱歉,但itoa()是一个你不会总能找到的扩展名

< stdlib.h> 。

#include< string.h> / *表示memcpy(),strcpy(),strcat()* /
#define NAMLEN 8
#define EXTLEN 3
#define MAX 512
#define MAXCOL 1
typedef struct {
char name [NAMLEN];
char ext [EXTLEN];
} NameExt;
NameExt NameArray [MAX + 1];
int NameCnt = 0;
main()


如果你想确定这也适用于C99编译器你必须至少告诉编译器回报main()的类型(

必须是int),因为旧的默认假设,没有声明的返回类型的函数

将返回int不再存在。

还告诉编译器参数的数量是合理的,所以

你最好这样做


int main(void)



int main(int argc,char * argv [])

{
//...
char fmtstr [32],digits [12];
int i;
char NAM [NAMLEN + 1],EXT [EXTLEN + 1];
int colnum = 0;
// ...
/ *用(测试)名称填充数组&扩展,
左对齐&空间充满(没有(字符)0')。
在真正的应用程序(...'代码)中,有findfirst(),
findnext(),排序,等。* /
memcpy(& NameArray [0]," One 1",11);


这显然假设NameExt结构的成员已经打包,即''ext''成员在''name'之后立即开始'

成员之间没有任何填充。虽然你可能经常会因为编译器无法保证工作而无法保证工作,但是b
之间(和之后)插入尽可能多的填充字节会员

喜欢它。如果它确实你的计划不再工作了。所以你

更好地做到这一点


memcpy(& NameArray [0] .name," One" NAMLEN);

memcpy(& NameArray [0] .ext," 1",EXTLEN);

memcpy(& NameArray [1]," TwoTwoTw222",11);
memcpy(& NameArray [2]," 333 003",11);
NameCnt = 3;
// ...
/ *打印名称&多列中的扩展名* /
//构造格式字符串,如%8s%c%3s
strcpy(fmtstr,"%");
strcat(fmtstr,itoa( NAMLEN,数字,10));


itoa()不是标准的C函数,因此在系统上会失败

那里没有ito()。

strcat(fmtstr," s%c%");
strcat(fmtstr,itoa(EXTLEN,digits,10));
strcat(fmtstr," s") ;


为什么不用sprintf()来组成格式字符串?


sprintf(fmtstr," %%% ds %% c %%% ds" NAMLEN,EXTLEN);

//按照指定的格式打印
for(i = 0; i< NameCnt; i ++){
memcpy(NAM,NameArray [i] .name,NAMLEN);
NAM [NAMLEN] =(char)0;


"(char)0"可能更好地写成''\'''',至少那么所有具有一定经验的C $程序员都会知道你的意思。

memcpy(EXT,NameArray [i] .ext,EXTLEN);
EXT [EXTLEN] =(char)0;
printf(fmtstr,NAM,''。'',EXT);
//在此处理列号
if(colnum ++< MAXCOL){
printf("");
} else {
printf(" \ n");
colnum = 0;
}
}
printf(" \ n");
// ...
}
---------------------- snip ----------------------- -
特别是,有更简单的方法:
(1)初始化NameArray记录;


或许,但这取决于你想要在实际情况下初始化数组

元素。因为我不知道什么是findfirst()和

findnext()返回(它们不是标准的C函数)所以

是不可能的。有一种更简单的方法可以为结构成员赋值。

(2)指定格式字符串,假设我想坚持使用常量
作为字段宽度;和
(3)printf NameArray array-of-char字段没有明确地将它们转换为字符串?


你实际上不需要这样做,因为你指定它们的长度,

所以即使没有终止''\ 0''字符它将正常工作。

你可以放心地打印


printf(fmtstr,NameArry [0] .name,''。'',NameArray [0 ] .ext);


当''fmtstr''是例如%8s%c%3s。


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);

另外,有没有办法获得例如,
sizeof NameExt.Ext
(这是行不通的)在编译时?
Newbie-ish questions - I''ve been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways
to do what this does. It does compile & run for me (with PowerC, a
16-bit DOS compiler); if there are nonstandard or "accidentally-works"
aspects, please let me know. {This is the sort of situation where if I knew *what* to Google for, I
wouldn''t *need* to... <grin>} Code follows: ---------------------- snip ------------------------
#include <stdio.h>
#include <stdlib.h> /* for itoa */
Sorry, but itoa() is an extension that you won''t always find in
<stdlib.h>.
#include <string.h> /* for memcpy(), strcpy(), strcat() */ #define NAMLEN 8
#define EXTLEN 3
#define MAX 512
#define MAXCOL 1 typedef struct {
char name[NAMLEN];
char ext[EXTLEN];
} NameExt; NameExt NameArray[MAX+1];
int NameCnt=0; main()
If you want to be sure that this also works with a C99 compiler you
must at least tell the compiler the return type of main() (which
must be int), since the old default assumption, that a function
without a declared return type will return int does not hold anymore.
Also telling the compiler the number of arguments is reasonable, so
you better make that either

int main( void )
or
int main( int argc, char *argv[ ] )
{
//...
char fmtstr[32], digits[12];
int i;
char NAM[NAMLEN+1], EXT[EXTLEN+1];
int colnum = 0; //...
/* 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);
This obviously assumes that the members of the NameExt structure are
packed, i.e. that the ''ext'' member starts immediately after the ''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 );
memcpy(&NameArray[1], "TwoTwoTw222", 11);
memcpy(&NameArray[2], "333 003", 11);
NameCnt = 3;
//... /* Print names & extensions in multiple columns */ // 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 ito().
strcat(fmtstr, "s%c%");
strcat(fmtstr, itoa(EXTLEN, digits, 10));
strcat(fmtstr, "s");
Why don''t you use sprintf() to make up the format string?

sprintf( fmtstr, "%%%ds%%c%%%ds" NAMLEN, EXTLEN );
// 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.
memcpy(EXT, NameArray[i].ext, EXTLEN);
EXT[EXTLEN] = (char)0;
printf(fmtstr, NAM, ''.'', EXT); // Handle columnizing here
if(colnum++ < MAXCOL) {
printf(" ");
} else {
printf("\n");
colnum = 0;
}
}
printf("\n");
//...
}
---------------------- snip ------------------------ Particularly, are there easier ways to:
(1) initialize the NameArray records;
Perhaps, but that will depend on what you want to initialize the array
elements with in the real case. Since I don''t know what findfirst() and
findnext() return (they aren''t standard C functions) it''s impossible to
say if there''s a simpler way to assign values to the structure members.
(2) specify the format string, assuming I want to stick with 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 them,
so even without the terminating ''\0'' characters it will work correctly.
You can safely do

printf( fmtstr, NameArry[ 0 ].name, ''.'', NameArray[ 0 ].ext );

when ''fmtstr'' is e.g. "%8s%c%3s".

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 );
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

问候, Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


感谢您的回复。

Thanks for replying.

#include< stdlib.h> / * for itoa * /
很抱歉,但itoa()是一个你不会总是在
< stdlib.h>中找到的扩展名。
#include <stdlib.h> /* for itoa */
Sorry, but itoa() is an extension that you won''t always find in
<stdlib.h>.



是的,我必须在我的编译器库中挖掘一下甚至找到它 -

我猜它是折旧的。甚至在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, 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?

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 using
sprintf() to format the entire output line, but not just the
format 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 my
compiler accepts charvar = 0x00 and charvar = \0, neither looked
"explicit" enought. I guess I''d forgotten you could use escaped
characters 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 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()?

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 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.)
(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 work correctly. 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, and

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]);
was just "over the top". <grin> C doesn''t have anything like
FORTRAN''s "implicit DO" does it?

I wasn''t sure "%8s" was safe, ''though I can''t remember now why I
thought 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工作,没有特定的数组索引?
< br $>
我肯定对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 does
sizeof(int) work? It''s not a "real instance". Is that the
sizeof paren/no paren thing?

Why does NameArray->ext work, without a specific array index?

And I''m definitely confused by NameArray.ext, *NameArray.ext [or
is that *(NameArray).ext?], and NameArray->ext. Could you kindly
explain 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''t
know 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 functions
the 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 reproduces
a functionality you already have with sprintf(). I guess someone
invented it for symmetry reasons because there''s atoi(), But atoi()
is a rather useless function anyway and should be avoided in favour
of 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 extensions
for your OS - the word "directory" doesn''t even appear anywhere in
the 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 of
variables. If you want to use types they must be enclosed in
parenthesis.
Why does NameArray->ext work, without a specific array index?
Because NameArray is automatically taken here to be a pointer to
the first element of the NameArray array (like when you pass an
array 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 like

struct xyz {
int x;
char y;
}

struct xyz a[ 10 ];

then ''a'' "decays" in situations where it''s used as a value into a
pointer to the first element of the array. Thus ''a->y'' is the same
as ''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.de



It doesn''t matter if you have an array of ints or structures or unions
or function pointers or whatever, it''s always the same. When the name
of 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 to
the first element of the array, no matter what kind of elements the
array has. See also what Chris Torek calls "The Rule":

http://web.torek.net/torek/c/index.html

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


这篇关于请帮助优化(和标准化)此代码...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆