请验证代码 [英] please verify the code

查看:55
本文介绍了请验证代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,全部!


我编写的函数解析完全绗缝的域名并提取

''host''和''domain''零件分开。对我来说最重要的是保持

原始字符串(在函数中用fqdn指向)。我的编码方式

是否正确,是否有一种方法可以优化功能并使其不那么笨拙(我喜欢K& R在其着名书籍中生成的代码) :简短,明确,

易于理解,没有多余的:))。


#include< stdio.h>

#包括< stdlib.h>

#include< string.h>


#define BUFLEN 1024


static int

parse_fqdn(char * fqdn,char * host,char * domain)

{

char p [BUFLEN],* s ;


strcpy(p,fqdn);

if((s = strstr(p,"。"))){

* s =''\ 0'';

strcpy(host,p);

strcpy(domain,++ s);

返回0;

}

其他

返回-1;

}


int main(无效)

{

char dom [] =" www.my.example.dom.ain" ;;

char h [BUFLEN],d [BUFLEN];


if(parse_fqdn(dom,h,d)== 0)

printf(" fqdn =''%s''\\\
hostname =''%s'',domain name =''%s''\ n,dom,h,d);

返回0;

}


提前致谢


最诚挚的问候,Roman Mashak。电子邮件: mr*@tusur.ru

Hello, All!

I wrote function parsing the fully-quilified domain name and extracting
''host'' and ''domain'' parts seperately. The important thing for me is to keep
original string (pointed by ''fqdn'' in function). Is my way of coding
correct, is there a way to optimize function and make it not so clumsy (I
like the code produced by K&R in their famous book: brief, clear,
comprehensible, nothing superfluous :) ).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFLEN 1024

static int
parse_fqdn(char *fqdn, char *host, char *domain)
{
char p[BUFLEN], *s;

strcpy(p, fqdn);
if ( (s = strstr(p, ".")) ) {
*s = ''\0'';
strcpy(host, p);
strcpy(domain, ++s);
return 0;
}
else
return -1;
}

int main(void)
{
char dom[] = "www.my.example.dom.ain";
char h[BUFLEN], d[BUFLEN];

if ( parse_fqdn(dom, h, d) == 0 )
printf("fqdn=''%s''\nhostname=''%s'', domain name=''%s''\n", dom, h, d);
return 0;
}

Thanks in advance

With best regards, Roman Mashak. E-mail: mr*@tusur.ru

推荐答案

Roman Mashak写道:
Roman Mashak wrote:
大家好!

我编写的函数解析完全绗缝的域名并提取
''host''和''domain' '部分分开。对我来说重要的是保持原始字符串(在函数中用fqdn指向)。我的编码方式是否正确,有没有办法优化功能并使其不那么笨拙(我喜欢K& R在他们着名的书中生成的代码:简短,清晰,可理解,没有多余的:))。

#include< stdio.h>
#include< stdlib.h>
#include< string.h>

#define BUFLEN 1024

parse_fqdn(char * fqdn,char * host,char * domain)
{
char p [BUFLEN],* s;
想想

1. 1024个字符在本地变量区域放置很多。

2.一个变量声明每行。

3.变量''p'是否必要?

4.如果参数指向一个常量字符串或

该函数不会修改字符串,请考虑

将参数声明为指向const char的指针。

strcpy(p,fqdn);
想想

1.当fqdn的长度大于BUFLEN时会发生什么?

2. fqdn时会发生什么是NULL?

if((s = strstr(p,"。"))){
想想

1.为了防止类型和逻辑错误,将函数的结果

与变量或常量进行比较。

* s =''\ 0'';
strcpy(host,p);
strcpy(domain,++ s);
返回0;
}

返回-1;
}

int main(void)
{
char dom [] =" www.my.example.dom.ain" ;;
char h [ BUFLEN],d [BUFLEN];

if(parse_fqdn(dom,h,d)== 0)
printf(" fqdn =''%s''\ nnhostname = ''%s'',域名=''%s''\ n,dom,h,d);
返回0;
虽然return 0;是有效的,考虑使用

EXIT_SUCCESS或EXIT_FAILURE。这两个命名常量

使代码更具可读性。

}

提前致谢

最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru
Hello, All!

I wrote function parsing the fully-quilified domain name and extracting
''host'' and ''domain'' parts seperately. The important thing for me is to keep
original string (pointed by ''fqdn'' in function). Is my way of coding
correct, is there a way to optimize function and make it not so clumsy (I
like the code produced by K&R in their famous book: brief, clear,
comprehensible, nothing superfluous :) ).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFLEN 1024

static int
parse_fqdn(char *fqdn, char *host, char *domain)
{
char p[BUFLEN], *s; Think about:
1. 1024 chars is a lot to place on the local variable area.
2. One variable declaration per line.
3. Is the variable ''p'' necessary?
4. If a parameter points to a constant string or
the function will not modify the string, consider
declaring the parameter as a pointer to a const char.

strcpy(p, fqdn); Think about:
1. What happens when the length of fqdn is greater than BUFLEN?
2. What happens when fqdn is NULL?
if ( (s = strstr(p, ".")) ) { Think about:
1. To prevent typeos and logic errors, compare the result
of a function to a variable or constant.
*s = ''\0'';
strcpy(host, p);
strcpy(domain, ++s);
return 0;
}
else
return -1;
}

int main(void)
{
char dom[] = "www.my.example.dom.ain";
char h[BUFLEN], d[BUFLEN];

if ( parse_fqdn(dom, h, d) == 0 )
printf("fqdn=''%s''\nhostname=''%s'', domain name=''%s''\n", dom, h, d);
return 0; Although "return 0;" is valid, consider using
EXIT_SUCCESS or EXIT_FAILURE. These two named constants
make the code more readable.
}

Thanks in advance

With best regards, Roman Mashak. E-mail: mr*@tusur.ru



-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com /〜scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.comeaucomputing.com/learn/faq/

其他网站:
http://www.josuttis.com - C ++ STL Library book
http://www.sgi。 com / tech / stl - 标准模板库


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


你好,托马斯!

你在星期五写道, 2005年12月16日21:27:15 -0800:


??>> parse_fqdn(char * fqdn,char * host,char * domain)

??>> {

??>> char p [BUFLEN],* s;

TM>想想

TM> 1. 1024个字符在本地变量区域放置很多。

[OT]

AFAIR完全quilifed的最大长度远小于1024

[/ OT]

TM> 2.每行一个变量声明。

这只是编码风格,不是吗? :)

TM> 3.变量''p'是否必要?

我用它来保存原始字符串(fqdn)的备份副本,有没有办法

来保存它没有定义额外的变量?

TM> 4.如果参数指向常量字符串或

TM>该函数不会修改字符串,请考虑

TM>将参数声明为指向const char的指针。

正确点,谢谢

??>> strcpy(p,fqdn);

TM>想想

TM> 1.当fqdn的长度大于BUFLEN时会发生什么?

TM> 2.当fqdn为NULL时会发生什么?

正确,我必须检查这些条件

??>> if((s = strstr(p,"。"))){

TM>想想

TM> 1.为了防止类型和逻辑错误,比较结果

TM>函数变量或常数。

你在这里是什么意思,请澄清。

最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru
Hello, Thomas!
You wrote on Fri, 16 Dec 2005 21:27:15 -0800:

??>> parse_fqdn(char *fqdn, char *host, char *domain)
??>> {
??>> char p[BUFLEN], *s;
TM> Think about:
TM> 1. 1024 chars is a lot to place on the local variable area.
[OT]
AFAIR the maximum length of fully-quilifed is much less than 1024
[/OT]
TM> 2. One variable declaration per line.
It''s simply coding style, isn''t it? :)
TM> 3. Is the variable ''p'' necessary?
I use it to keep the backup copy of original string (fqdn), is there a way
to preserve it not defining extra variable?
TM> 4. If a parameter points to a constant string or
TM> the function will not modify the string, consider
TM> declaring the parameter as a pointer to a const char.
correct point, thanks
??>> strcpy(p, fqdn);
TM> Think about:
TM> 1. What happens when the length of fqdn is greater than BUFLEN?
TM> 2. What happens when fqdn is NULL?
correct, I have to check these conditions
??>> if ( (s = strstr(p, ".")) ) {
TM> Think about:
TM> 1. To prevent typeos and logic errors, compare the result
TM> of a function to a variable or constant.
What do you mean here, please clarify.
With best regards, Roman Mashak. E-mail: mr*@tusur.ru


2005年12月16日星期五21:27:15 -0800,Thomas Matthews

< Th *************** @ cox.network>在comp.lang.c中写道:
On Fri, 16 Dec 2005 21:27:15 -0800, Thomas Matthews
<Th***************@cox.network> wrote in comp.lang.c:
Roman Mashak写道:
Roman Mashak wrote:




[large snip]



[large snip]

return 0;
return 0;


虽然return 0;有效,请考虑使用
EXIT_SUCCESS或EXIT_FAILURE。这两个命名的常量
使代码更具可读性。


Although "return 0;" is valid, consider using
EXIT_SUCCESS or EXIT_FAILURE. These two named constants
make the code more readable.




声称返回0;这是一种迂腐的理由。比return EXIT_SUCCESS;更少可读b $ b;任何远程有资格的人都可以阅读C代码。


我_never_在程序中使用EXIT_SUCCESS,除非我也使用

EXIT_FAILURE, OP的节目没有。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

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 ++
http:/ /www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



It is pedantic beyond reason to claim that "return 0;" is less
readable than "return EXIT_SUCCESS;" to anyone remotely qualified to
read C code.

I _never_ use EXIT_SUCCESS in a program unless I also use
EXIT_FAILURE, which the OP''s program does not.

--
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++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于请验证代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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