本文介绍了通过价值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C Unleashed一书的第2章中,作者声明这个

代码


#include< stdio.h

无效增量(char * p)

{

++ p

}


int main(无效)

{

char * s =" Hello world";

increment(s);

printf("%s \ n",s);

返回0;

}


不起作用(事实上导致未定义的行为):


修改后的代码可以正常工作,即打印ello world的代码;

未显示。


修改代码的正确方法是什么,以便ello world是打印了吗?


Robert Wishlaw

In Chapter 2 of the book ''C Unleashed'' the author states that this
code

#include <stdio.h
void increment(char *p)
{
++p
}

int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}

"doesn''t work (and in fact results in undefined behavior):"

Modified code that does work, that is, code that prints "ello world"
is not shown.

What is is proper way to modify the code so that "ello world" is
printed?

Robert Wishlaw

推荐答案



您的意思是#include< stdio.h?

Did you mean #include <stdio.h ?



您的意思是++ p; ?

Did you mean ++p; ?



未定义的行为?它原来是++ * p; ?

Undefined behaviour? Was it originally ++*p; ?



''正确''是相对的,但以下就足够了......


无效增量(char ** p )/ *指向指针的指针* /

{

++(* p); / *括号实际上是多余的* /

}


增量(& s);


-

Peter

''Proper'' is relative, but the following suffices...

void increment(char **p) /* pointer to a pointer */
{
++(*p); /* parentheses are actually redundant */
}

increment(&s);

--
Peter




我永远不会我以为理查德希思菲尔德会犯很多关于基本内容的错误。我想是生活和学习。

-

Ben Pfaff




您的意思是#include< stdio.h?


Did you mean #include <stdio.h ?



是。

Yes.



您的意思是++ p; ?


Did you mean ++p; ?



是。

Yes.



未定义的行为?它原来是++ * p; ?


Undefined behaviour? Was it originally ++*p; ?



''正确''是相对的,但以下就足够了......


无效增量(char ** p )/ *指向指针的指针* /

{

++(* p); / *括号实际上是多余的* /

}


增量(& s);


''Proper'' is relative, but the following suffices...

void increment(char **p) /* pointer to a pointer */
{
++(*p); /* parentheses are actually redundant */
}

increment(&s);



谢谢。

Thank you.



这篇关于通过价值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:55