递归输出 [英] recursion output

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

问题描述

任何人都可以解释这个函数的输出。

我无法理解它


void ret_str(char * s)

{

if(* s!=''\''')

// {

cout<< *( s);

ret_str(s + 1);

// cout<< *(s);

//}


}

can anyone explain the output of this function.
im having trouble comprehending it

void ret_str(char* s)
{
if(*s != ''\0'')
//{
cout<<*(s) ;
ret_str(s+1);
//cout<<*(s) ;
//}

}

推荐答案

" Lamefif" < La ***** @ gmail.comwrote in message

news:11 ********************** @ k79g2000hse。 googlegr oups.com ...
"Lamefif" <La*****@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...

任何人都可以解释这个函数的输出。

我无法理解它


void ret_str(char * s)

{

if(* s!=''\ 0'')

// {

cout<< *(s);

ret_str(s + 1);

// cout< < *(s);

//}


}
can anyone explain the output of this function.
im having trouble comprehending it

void ret_str(char* s)
{
if(*s != ''\0'')
//{
cout<<*(s) ;
ret_str(s+1);
//cout<<*(s) ;
//}

}



它应该只是输出一个字符串。


char * s是一个char指针,在这种情况下它将指向一个

c风格字符串的开头。一个以null \0结尾的字符数组。

现在,就你的情况而言,它会向前打印字符串。如果s指向的

字符不是结尾,它将输出

字符,然后用指针递增1来调用自身。

例如,说字符串是:

" hello"这将包括''h''''''''''''''''''''\\'''叫做s指的是''h''。因为它不是\0它

输出''h'',然后用s + 1调用自己,或者指向''e''这个

继续,直到到达null字符,它只是返回。


如果cout在递归调用之后,那么它将向后打印字符串

。阅读器的一个练习,以确定原因。

It should just output a string.

char* s is a char pointer, in this case it will point to the beginning of a
c-style string. An array of characters terminated by a null \0.

Now, in your case, as it is, it will print the string forwards. If the
character that s is pointing to is not the end, it will output the
character, then call itself with the pointer incremented by 1.
For example, say the string is:
"hello" which would consist of ''h'' ''e'' ''l'' ''l'' ''o'' ''\0''
First time it''s called s is pointing to the ''h''. Since it''s not \0 it
outputs the ''h'', then calls itself with s+1, or pointing to the ''e'' This
continues until the null character is reached where it simply returns.

If the cout is after the recursion call, then it would print the string
backwards. An excercise to the reader to determine why.


这也是我的想法,这就是为什么我不理解这个

输出:


你好? @? @? 0 @? 12crtexe.c__native_startup_state

== __initia

lizedUnknown运行时检查错误

_alloca周围的堆栈内存已损坏

本地变量在初始化之前使用

堆栈内存已损坏

对较小数据类型的强制转换导致数据丢失。如果这是

intenta

l,你应该使用适当的

位掩码掩盖演员的来源。对于exa

mple:

char c =(i& 0xFF);

以这种方式更改代码不会影响质量

得到的优化

zed代码。

dd在函数调用中没有正确保存ESP的值。

这是usua

lly调用一个函数声明的函数调用

约定af

使用不同的调用声明的函数常规。

变量''''周围的堆栈已损坏。变量''''

正在使用wi

正在初始化.Run-Time Check Failure#%d - %sUnknown Module

NameUnknow

n FilenameRun-Time Check Failure#%d - %sRuntime Check Error。

无法显示RTC Message.Stack损坏附近未知

variableStack区域a

round _alloca内存被此函数保留已损坏

%s %s%s%s>

%s%s%p%s%ld%s%d%s周围区域这个

函数保留的_alloca内存是c

orrupted

地址:0x

大小:

此功能中的分配号码:

数据:< wsprintfAuser32.dll%。2X正在使用变量而不是

初始化

d。堆积_alloca corruptedLocal变量之前使用

initializationStack m

emory corruptionCast到较小的类型导致dataStack指针丢失

corruptio

ny A? A£A A?A?A?A _controlfp_s(((void *)0),0x00010000,

0x00030000)_setd

efaultprecisionintel

\fp8.cMSPDB80.DLLrPDBOpenValidate5EnvironmentDirec torySOFTWA

RE\Microsoft\VisualStudio\8.0\Setup

\VSRegCloseKeyRegQueryValueExARegOpenKeyExAAD

VAPI32 .DLLRSDS? ?7v)M?o?/ _ 1,

that''s what i was thinking too, thats why i dont understand this
output:

hello ? @? @? 0 @? 12crtexe.c__native_startup_state
== __initia
lizedUnknown Runtime Check Error
Stack memory around _alloca was corrupted
A local variable was used before it was initialized
Stack memory was corrupted
A cast to a smaller data type has caused a loss of data. If this was
intentiona
l, you should mask the source of the cast with the appropriate
bitmask. For exa
mple:
char c = (i & 0xFF);
Changing the code in this way will not affect the quality of the
resulting optim
ized code.
dd The value of ESP was not properly saved across a function call.
This is usua
lly a result of calling a function declared with one calling
convention with a f
unction pointer declared with a different calling convention.
Stack around the variable '''' was corrupted.The variable '''' is
being used wi
thout being initialized.Run-Time Check Failure #%d - %sUnknown Module
NameUnknow
n FilenameRun-Time Check Failure #%d - %sRuntime Check Error.
Unable to display RTC Message.Stack corrupted near unknown
variableStack area a
round _alloca memory reserved by this function is corrupted
%s%s%s%s>
%s%s%p%s%ld%s%d%sStack area around _alloca memory reserved by this
function is c
orrupted
Address: 0x
Size:
Allocation number within this function:
Data: <wsprintfAuser32.dll%.2X A variable is being used without being
initialize
d.Stack around _alloca corruptedLocal variable used before
initializationStack m
emory corruptionCast to smaller type causing loss of dataStack pointer
corruptio
ny A? A£ Ah A@ Aá?A°?A _controlfp_s(((void *)0), 0x00010000,
0x00030000)_setd
efaultprecisionintel
\fp8.cMSPDB80.DLLrPDBOpenValidate5EnvironmentDirec torySOFTWA
RE\Microsoft\VisualStudio\8.0\Setup
\VSRegCloseKeyRegQueryValueExARegOpenKeyExAAD
VAPI32.DLLRSDS ? ?7v)M?o ?/_ 1,


Lamefif写道:
Lamefif wrote:

that这也是我的想法,这就是为什么我不理解这个

输出:
that''s what i was thinking too, thats why i dont understand this
output:



你在想什么?请在回复中保留上下文。

What were you thinking? Please retain context in your replies.


hello? @? @? 0 @? 12crtexe.c__native_startup_state
hello ? @? @? 0 @? 12crtexe.c__native_startup_state



你得到了垃圾输出,因为你注释掉了大括号,所以你得到了无限递归




-

Ian Collins。

You get crap output because you commented out the braces, so you get
infinite recursion.

--
Ian Collins.


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

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