试着阅读/理解这段代码 [英] Tryiing just to read/understand this code

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

问题描述

以下是来自string.c的部分

linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat

我想完全理解。我不完全理解LINE 514;不要说b $ b提到整个内在的循环以及它正在完成的东西。我想b / b
如果我至少可以理解这个strstr方法的每一行和

为什么它写的是它写的方式,以及关于性能或

代码简单,它会帮助我学习c.Thanks。 497 char *

498 strstr(const char * as1,const char * as2)

499 {

500 const char * s1,* s2 ;

501 const char * tptr;

502 char c;

503

504 s1 = as1;

505 s2 = as2;

506

507 if(s2 == NULL || * s2 ==''\''')

508返回((字符*)s1);

509 c = * s2;



511 while(* s1)

512 if(* s1 ++ == c){

513 tptr = s1;

514 while((c = * ++ s2)== * s1 ++&& c)

515;

516 if(c == 0)

517 return((char *)tptr - 1);

518 s1 = tptr;

519 s2 = as2;

520 c = * s2 ;

521}

522

523返回(NULL);

524}

解决方案



smnoff写道:


以下是string.c的一节在这个

linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat

我想完全理解。我不太了解LINE 514;不要说b $ b提到整个内在的循环以及它正在完成的东西。我想b / b
如果我至少可以理解这个strstr方法的每一行和

为什么它写的是它写的方式,以及关于性能或

代码简单,它会帮助我学习c.Thanks。 497 char *

498 strstr(const char * as1,const char * as2)

499 {

500 const char * s1,* s2 ;

501 const char * tptr;

502 char c;

503

504 s1 = as1;

505 s2 = as2;

506

507 if(s2 == NULL || * s2 ==''\''')

508返回((字符*)s1);

509 c = * s2;



511 while(* s1)

512 if(* s1 ++ == c){

513 tptr = s1;

514 while((c = * ++ s2)== * s1 ++&& c)

515;

516 if(c == 0)

517 return((char *)tptr - 1);

518 s1 = tptr;

519 s2 = as2;

520 c = * s2 ;

521}

522

523 return(NULL);

524}



将上面的函数包装在main中,然后只包含vi当地人在一个

调试器中。这是了解正在发生的事情的最佳方式。另外,它只是基本的指针数学,所以你可能会写一些小程序来实现你的指针数学。


Brian


2006-08-05,smnoff< rh ****** @ hotmail.comwrote:
< blockquote class =post_quotes>
以下是来自string.c的部分

linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common /util/string.cthat

我想完全理解。我不完全理解LINE 514;不要说b $ b提到整个内在的循环以及它正在完成的东西。我想b / b
如果我至少可以理解这个strstr方法的每一行和

为什么它写的是它写的方式,以及关于性能或

代码简单,它会帮助我学习c.Thanks。 497 char *


498 strstr(const char * as1,const char * as2)



我们正在寻找字符串as1中的一系列字符匹配

as2。


499 {

500 const char * s1,* s2;

501 const char * tptr;

502 char c;

503

504 s1 = as1;

505 s2 = as2;

506

507 if(s2 == NULL || * s2 ==''\''')

508 return((char *)s1);

509 c = * s2;

510

511 while(* s1)

512 if(* s1 ++ == c){

513 tptr = s1;



此时*(s1 - 1)和* s2都包含相同

字符的副本。所以这是一个匹配的候选者,但我们需要检查是否

如果我们

将两个指针继续指向相同的字符到s2中最后一个

非空终止字符的时间。


strstr(" catenation"," nat")应匹配,但是strstr (catentation,

" natj")应该返回NULL。在这一点上,我们有s1-1指向catenation的

n。和s2指向nat的n。我们需要通过这两个字符串走

,以匹配''''和''''并找到我们的方式

到''j' '。


我们可以像下面这样更可读地写下一点:


const char * p = s1 - 1;

const char * q = s2;

size_t i;


for(i = 0; q [i]!=''\\ \\ 0''; i ++)

if(p [i]!= q [i])

返回NULL; / *部分匹配,但不够好* /


/ *如果我们到达这里,那么as2是as1的子字符串... * /

< blockquote class =post_quotes>
514 while((c = * ++ s2)== * s1 ++&& c)

515;



所以,我们在做同样的事情:我们将s2和s1增加为

,因为它们继续指向等于字节,并且s2没有得到null

终结符。 514行和515行基本上是伪代码:


永远

增量s2

让c = * s2


如果c!= s1

增量s1

休息

endif


增量s1

如果c ==''\'''中断

endfor


In你的伪代码,为什么是


如果c!= s1


使用!=而不是==喜欢iin的实际代码?


> 514((c = * ++ s2)== * s1 ++&& c)
515;



所以,我们在做同样的事情:我们将s2和s1增加为

,因为它们继续指向等于字节,并且s2没有得到null

终结符。 514行和515行基本上是伪代码:


永远

增量s2

让c = * s2


如果c!= s1

增量s1

休息

endif


增量s1

如果c ==''\'''中断

endfor



Below is a section from string.c at this
linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat
I am trying to fully understand.I don''t fully understand LINE 514; not to
mention that entire inner while loop and what it''strying to accomplish. I
figured that if I can at least understand each line of this strstr methodand
why it''s written the ways it written, as well as in regards to perfomance or
code simplicity, it will help me learn c.Thanks. 497 char *
498 strstr(const char *as1, const char *as2)
499 {
500 const char *s1, *s2;
501 const char *tptr;
502 char c;
503
504 s1 = as1;
505 s2 = as2;
506
507 if (s2 == NULL || *s2 == ''\0'')
508 return ((char *)s1);
509 c = *s2;
510
511 while (*s1)
512 if (*s1++ == c) {
513 tptr = s1;
514 while ((c = *++s2) == *s1++ && c)
515 ;
516 if (c == 0)
517 return ((char *)tptr - 1);
518 s1 = tptr;
519 s2 = as2;
520 c = *s2;
521 }
522
523 return (NULL);
524 }

解决方案


smnoff wrote:

Below is a section from string.c at this
linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat
I am trying to fully understand.I don''t fully understand LINE 514; not to
mention that entire inner while loop and what it''strying to accomplish. I
figured that if I can at least understand each line of this strstr methodand
why it''s written the ways it written, as well as in regards to perfomance or
code simplicity, it will help me learn c.Thanks. 497 char *
498 strstr(const char *as1, const char *as2)
499 {
500 const char *s1, *s2;
501 const char *tptr;
502 char c;
503
504 s1 = as1;
505 s2 = as2;
506
507 if (s2 == NULL || *s2 == ''\0'')
508 return ((char *)s1);
509 c = *s2;
510
511 while (*s1)
512 if (*s1++ == c) {
513 tptr = s1;
514 while ((c = *++s2) == *s1++ && c)
515 ;
516 if (c == 0)
517 return ((char *)tptr - 1);
518 s1 = tptr;
519 s2 = as2;
520 c = *s2;
521 }
522
523 return (NULL);
524 }

Wrap the function above in a main then just view the locals in a
debugger. That''s the best way to learn what is happening. Also, it''s
just basic pointer math, so you might to write some small programs to
practice your pointer math.

Brian


On 2006-08-05, smnoff <rh******@hotmail.comwrote:

Below is a section from string.c at this
linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat
I am trying to fully understand.I don''t fully understand LINE 514; not to
mention that entire inner while loop and what it''strying to accomplish. I
figured that if I can at least understand each line of this strstr methodand
why it''s written the ways it written, as well as in regards to perfomance or
code simplicity, it will help me learn c.Thanks. 497 char *

498 strstr(const char *as1, const char *as2)

We''re looking for a sequence of characters in the string as1 that match
as2.

499 {
500 const char *s1, *s2;
501 const char *tptr;
502 char c;
503
504 s1 = as1;
505 s2 = as2;
506
507 if (s2 == NULL || *s2 == ''\0'')
508 return ((char *)s1);
509 c = *s2;
510
511 while (*s1)
512 if (*s1++ == c) {
513 tptr = s1;

At this point *(s1 - 1) and *s2 both contain copies of the same
character. So this is a candidate for a match, but we need to check if
the two pointers carry on pointing to the same characters if we
increment them each one byte at a time until the last
non-null-terminating character in s2.

strstr("catenation", "nat") should match, but strstr("catentation",
"natj") should return NULL. At this point we''ve got s1-1 pointing to the
n of "catenation" and s2 pointing to the n of "nat". We need to walk
through both strings, to match up the ''a'' and the ''t'' and find our way
to the ''j''.

We could write the next bit perhaps more readably like this:

const char *p = s1 - 1;
const char *q = s2;
size_t i;

for (i = 0; q[i] != ''\0''; i++)
if (p[i] != q[i])
return NULL; /* partial match, but not good enough */

/* If we get here then as2 is a substring of as1 ... */

514 while ((c = *++s2) == *s1++ && c)
515 ;

So, here we''re doing the same thing: we increment s2 and s1 for as long
as they continue to point to equal bytes, and s2 hasn''t got to a null
terminator. Lines 514 and 515 mean basically this in pseudocode:

forever
increment s2
let c = *s2

if c != s1
increment s1
break
endif

increment s1
if c == ''\0'' break
endfor


In your pseudocode, why is the

if c != s1

use the != as opposed to the == like iin the actual code?

> 514 while ((c = *++s2) == *s1++ && c)
515 ;


So, here we''re doing the same thing: we increment s2 and s1 for as long
as they continue to point to equal bytes, and s2 hasn''t got to a null
terminator. Lines 514 and 515 mean basically this in pseudocode:

forever
increment s2
let c = *s2

if c != s1
increment s1
break
endif

increment s1
if c == ''\0'' break
endfor



这篇关于试着阅读/理解这段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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