功能说明 [英] function explanation

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

问题描述



这个函数的作用是什么?


void test_function(char * s,char * t){


while(* s ++ = * t ++)


;


}

-

通过 http://dbforums.com 发布

解决方案



" dominant" <我********* @ dbforums.com>在消息中写道

新闻:33 **************** @ dbforums.com ...


什么这个函数呢?

void test_function(char * s,char * t){

while(* s ++ = * t ++)

;

}

-
通过 http: //dbforums.com





" dominant" <我********* @ dbforums.com>在消息中写道

新闻:33 **************** @ dbforums.com ...


什么这个函数呢?

void test_function(char * s,char * t){

while(* s ++ = * t ++)

; <


strcpy()


-Mike




" dominant" <我********* @ dbforums.com>在消息中写道

新闻:33 **************** @ dbforums.com ...


什么这个函数呢?

void test_function(char * s,char * t){

while(* s ++ = * t ++);




请记住=运算符返回分配了

的值。还要记住当控制表达式返回时,while循环

终止

0.


然后考虑C中的字符串。一堆

个字符后跟一个空字符。 null

字符的值为0.


(请注意,当s和t递增时,确切地说不是

a直接问题很多初学者所以这是

略显非正式,但对你来说可能已经足够好了)


s和t中的第一个是递增的,即。他们被指向

指向他们指向的字符串中的下一个字符

to。然后为*运算符

返回它们的旧值以供使用。这意味着值t(因为这是在增量之后是
)指向现在分配给指向的位置

s。然后通过while

循环检查该值,以查看该值是否为0。如果它不是零那么

循环再次进行,但这次s和t指向

到不同的位置。所以当t指向一个null

终止符时,这个值(也就是0)被分配给* s而while

循环得到一个0并终止。


while循环也可以这样写:


while(* s = * t)

{

t ++;

s ++;

}


请注意,您发布的版本可能指向

在它指向的数组之外[1]。然而这是合法的

,因为标准允许这是为了迎合上述成语和

的朋友。如果它指向某个合法的地方,你甚至不允许取消引用这个指针




[1]它实际上是指向数组中的元素,但是你得到了

我的漂移。


-

托马斯。



What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++)

;

}
--
Posted via http://dbforums.com

解决方案


"dominant" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...


What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++)

;

}
--
Posted via http://dbforums.com




"dominant" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...


What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++)

;

}



strcpy()

-Mike



"dominant" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...


What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++);



Remember that the = operator returns the value that
was assigned. Also remember that thew while loop
terminates when the controlling expression returns
0.

Then consider strings in C. They consist of a bunch of
characters followed by a null character. The null
character has value 0.

(Note that exactly when s and t are incremented is not
a straightforward issue for many beginners so this is
slightly informal, but is probably good enough for you)

First of s and t are incremented, ie. they are made to
point to the next char in the string they are pointing
to. Then their old value is returned for the * operator
to use. This means that value t was (since this is after
the increment) pointing to is now assigned to the position
s was pointing to. This value is then inspected by the while
loop to see if this value is 0 or not. If it is non-zero then
the loop goes again, but this time s and t are pointing
to different positions. So when t is pointing to a null
terminator, this value (aka 0) is assigned to *s and the while
loop gets a 0 and terminates.

The while loop can also be written like this:

while(*s = *t)
{
t++;
s++;
}

Note that the version you posted makes t potentially point
outside the array it is pointing to[1]. This is however legal
as the standard allows this to cater for the above idiom and
friends. You are not allowed to dereference this pointer even
if it is pointing somewhere legal.

[1] It is actually pointing to elements in the array, but you get
my drift.

--
Thomas.


这篇关于功能说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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