strupr尸检分析 [英] strupr Postmortem Analysis

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

问题描述

我找到了一个上层函数的字符串,如下所示:


char * strupr(char * string)

{

char * s;

if(string)

{

for(s = string; * s; ++ s)

* s = toupper(* s);

}

返回字符串;

}


它有效,一切正常,但我认为我可以将它减少到

之后无效:


char * strupr(char * string)

{

// char * s;

if(string)

{

// for(s = string; * s; ++ s)

for(string; * string; ++ string)

// * s = toupper(* s);

* string = toupper(* string);

}

返回字符串;

}


有人可以解释为什么底部功能不起作用?谢谢!

解决方案

< hu ************ @ yahoo.comwrote:


我找到了一个上层函数的字符串,如下所示:


char * strupr(char * string)

{

char * s;

if(string)

{

for(s = string ; * s; ++ s)

* s = toupper(* s);

}

返回字符串;

}


它有效,一切正常,但我认为我可以将它减少到

之后无效:


char * strupr(char * string)

{

// char * s;

if(string)

{

// for(s = string; * s; ++ s)

for(string; * string ; ++ string)

// * s = toupper(* s);

* string = toupper(* string);

}

返回字符串;

}


有人可以解释为什么底部函数会不行吗?谢谢!



是的。原始函数保留了字符串开头的副本

(''string''),并将其返回。在第二个函数中,你返回

修改后的指针,它现在指向终止的NUL字符。


-

Chris Smith - 首席软件开发人员/技术培训师

MindIQ Corporation


我找到了一个上层函数的字符串,如下所示:


>

char * strupr(char * string)

{

char * s;

if(string)

{

for(s = string; * s; ++ s)

* s = toupper(* s);

}

返回字符串;

}


它有效且一切正常,但我想我可以将它修剪到

之后不起作用:



line 1char * strupr(char * string)

第2行{

第3行// char * s;

第4行if(string )

第5行{

第6行//为(s = string; * s; ++ s)

第7行r(字符串; *串; ++ string)

第8行// * s = toupper(* s);

第9行* string = toupper(* string);

第10行}

第11行返回字符串;

第12行}


>

有人可以解释为什么底部功能不起作用?谢谢!



我看到问题的一部分:第9行,* string = toupper(* string);。我是

踩过指针string的内容,是吗?当编写

修改后的代码时,每个实例都通过for循环,* string

给出了一些新值然后中断,是吗?


如果有人可以引导我完成循环,那可能会让我感到很尴尬。我是新手指点......谢谢。


hu ************ @ yahoo.com 写道:


我找到了一个字符串上面的函数看起来像这样:

char * strupr(char * string)



以str开头的标识符;接下来是一个小写字母

为将来的字符串函数保留。


{

char * s;

if(string)

{

for(s = string; * s; ++ s)

* s = toupper(* s);



那应该是'* s = toupper((unsigned char)* s);",char可以是

签名并传递一个不适合未签名字符的值和

未定义EOF。


}

返回字符串;

}


它有效且一切正常,但我想我可以把它减少到

以下是行不通的:


char * strupr(char * string)

{

// char * s;

if(string)

{

// for(s = string; * s; ++ s)

for(string; * string; ++ string)

// * s = toupper(* s);

* string = toupper(* string) ;

}

返回字符串;

}


有人可以解释为什么底层函数没有不行吗?谢谢!



你的意思是不起作用?它的作用与

的预期不同?您使用的实际代码在哪里

演示了问题?


第二个函数在功能上与第一个函数不同;

第一个函数返回一个指向字符串开头的指针

提供,第二个函数返回一个指向

字符串结尾的指针。如果您正在做这样的事情:


char s [] =" test";

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


结果可能不会是你所期望的,而这是:


char s [] ="测试;

strupr(s);

printf("%s \ nn,s);


将按预期工作。


应该注意,因为函数对字符串

进行操作,前提是这样的事情会导致未定义的行为:


strupr(测试);


第一个功能可以简化为:


char * strupr(char * string)

{

char * s = string;

if(s)

for(s; * s; ++ s)

* s = toupper(* s);

返回字符串;

}


(有些人反对缺少括号,添加它们,因为你看到

适合。)


甚至:


char * strupr(char * string)

{

char * s = string;

if(s )

做{* s = toupper(* s); } while(* s ++);

返回字符串;

}


Robert Gamble


I found a string to upper function that looks like this:

char *strupr(char *string)
{
char *s;
if (string)
{
for (s = string; *s; ++s)
*s = toupper(*s);
}
return string;
}

It works and all is ok, but I thought I could trim it down to the
following that doesn''t work:

char *strupr(char *string)
{
// char *s;
if (string)
{
// for (s = string; *s; ++s)
for (string; *string; ++string)
// *s = toupper(*s);
*string = toupper(*string);
}
return string;
}

Can someone explain why the bottom function doesn''t work? Thanks!

解决方案

<hu************@yahoo.comwrote:

I found a string to upper function that looks like this:

char *strupr(char *string)
{
char *s;
if (string)
{
for (s = string; *s; ++s)
*s = toupper(*s);
}
return string;
}

It works and all is ok, but I thought I could trim it down to the
following that doesn''t work:

char *strupr(char *string)
{
// char *s;
if (string)
{
// for (s = string; *s; ++s)
for (string; *string; ++string)
// *s = toupper(*s);
*string = toupper(*string);
}
return string;
}

Can someone explain why the bottom function doesn''t work? Thanks!

Yes. The original function kept a copy of the beginning of the string
(''string''), and returned it. In the second function, you are returning
the modified pointer, which now points to the terminating NUL character.

--
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation


I found a string to upper function that looks like this:

>
char *strupr(char *string)
{
char *s;
if (string)
{
for (s = string; *s; ++s)
*s = toupper(*s);
}
return string;
}

It works and all is ok, but I thought I could trim it down to the
following that doesn''t work:

line 1char *strupr(char *string)
line 2{
line 3// char *s;
line 4 if (string)
line 5 {
line 6// for (s = string; *s; ++s)
line 7 for (string; *string; ++string)
line 8// *s = toupper(*s);
line 9 *string = toupper(*string);
line 10 }
line 11 return string;
line 12}

>
Can someone explain why the bottom function doesn''t work? Thanks!

I see part of the problem: line 9, "*string = toupper(*string);". I''m
stepping over the contents of the pointer "string", yes? As the
modified code is written, every instance through the for loop, *string
is given some new value which then breaks, yes?

If someone could step me through the loop that might clear things up
for me. I''m new to pointers... Thanks.


hu************@yahoo.com wrote:

I found a string to upper function that looks like this:

char *strupr(char *string)

Identifiers beginning with "str" followed by a lowercase letter are
reserved for future string functions.

{
char *s;
if (string)
{
for (s = string; *s; ++s)
*s = toupper(*s);

That should really be "*s = toupper((unsigned char)*s);", char can be
signed and passing a value that does not fit into an unsigned char and
is not EOF is undefined.

}
return string;
}

It works and all is ok, but I thought I could trim it down to the
following that doesn''t work:

char *strupr(char *string)
{
// char *s;
if (string)
{
// for (s = string; *s; ++s)
for (string; *string; ++string)
// *s = toupper(*s);
*string = toupper(*string);
}
return string;
}

Can someone explain why the bottom function doesn''t work? Thanks!

What do you mean by "doesn''t work"? What did it do differently than
what you expected? Where is the actual code that you are using that
demonstrates the problem?

The second function is not functionally identical to the first one; the
first function returns a pointer to the beginning of the string
provided, the second function returns a pointer to the end of the
string. If you are doing something like this:

char s[] = "test";
printf("%s\n", strupr(s));

the result probably won''t be what you expect whereas this:

char s[] = "test";
strupr(s);
printf("%s\n", s);

will work as expected.

It should be noted that since the function operates on the string
provided that something like this would result in undefined behavior:

strupr("test");

The first function can be reduced to something like this:

char *strupr(char *string)
{
char *s = string;
if (s)
for (s; *s; ++s)
*s = toupper(*s);
return string;
}

(Some people would object to the lack of brackets, add them as you see
fit.)

or even:

char *strupr(char *string)
{
char *s = string;
if (s)
do { *s = toupper(*s); } while (*s++);
return string;
}

Robert Gamble


这篇关于strupr尸检分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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