另一个String逆转问题 [英] Another String reversal question

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

问题描述

好像我们需要另一个字符串反转问题。


我对以下代码有疑问,我认为应该有效。


int StringReverse(char * psz)

{

char * p = psz;

char * q = psz + strlen(psz) - 1;

while(p< q)

{

char tmp = * p;

* p = * q;

* q = tmp;

p ++;

q--;

}

返回1;


}


事情是,当它到达* p = * q行时,我违反访问权限。


任何想法为什么,或者我能做些什么来解决这个问题?


谢谢,


Michael


[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

As if we needed another string reversal question.

I have a problem with the following code, that I believe should work.

int StringReverse(char* psz)
{
char *p = psz;
char *q = psz + strlen(psz) - 1;
while (p < q)
{
char tmp = *p;
*p = *q;
*q = tmp;
p++;
q--;
}
return 1;

}

Thing is, when it gets to the *p = *q line, I get an access violation.

Any ideas why, or what I can do to fix this?

Thanks,

Michael

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

Michael写道:
Michael wrote:

int StringReverse(char * psz)
{< br *> char * p = psz;
char * q = psz + strlen(psz) - 1;
while(p< q)
{
char tmp = * p;
* p = * q;
* q = tmp;
p ++;
q--;
}
返回1;

}

是的,当它到达* p = * q行时,我得到访问冲突。
int StringReverse(char* psz)
{
char *p = psz;
char *q = psz + strlen(psz) - 1;
while (p < q)
{
char tmp = *p;
*p = *q;
*q = tmp;
p++;
q--;
}
return 1;

}

Thing is, when it gets to the *p = *q line, I get an access violation.




强烈指示指针值是错误的

或您以无效方式使用该函数。

您的调试器说了什么?

-

Karl Heinz Buchegger
kb ****** @ gascad.at


Karl Heinz Buchegger写道:
Karl Heinz Buchegger wrote:
Michael写道:

Michael wrote:

int StringReverse(char * psz)
{*> char * p = psz;
char * q = psz + strlen(psz) - 1;
while (p < q)
{
char tmp = * p;
* p = * q;
* q = tmp;
p ++;
q--;
}
返回1;

}

是的,当它到达* p = * q行时,我得到访问冲突。
int StringReverse(char* psz)
{
char *p = psz;
char *q = psz + strlen(psz) - 1;
while (p < q)
{
char tmp = *p;
*p = *q;
*q = tmp;
p++;
q--;
}
return 1;

}

Thing is, when it gets to the *p = *q line, I get an access violation.



强烈指示指针值是错误的
或者你以无效方式使用该函数。
你的调试器说了什么?


A strong indication that either the pointer value is wrong
or you use the function in an invalid way.
What did your debugger say?




OP无法做*类似的事情:


char * some_str =" reverse me" ;;

int result = StringReverse(some_str);

...


雅思? ;-)


--ag

-

Artie Gold - 德克萨斯州奥斯汀
哦,对于普通老垃圾邮件的美好时光。



The OP couldn''t *possibly* done something like:

char * some_str = "reverse me";
int result = StringReverse(some_str);
...

Ya think? ;-)

--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


Artie Gold写道:
Artie Gold wrote:


Karl Heinz Buchegger wrote:
Michael写道:

Michael wrote:

int StringReverse(char * psz)
{
char * p = psz;
char * q = psz + strlen(psz) - 1;
while(p< q)
{
char tmp = * p;
* p = * q;
* q = tmp;
p ++;
q--;
}
返回1;

}

是的,当它到达* p = * q行时,我得到访问冲突。
int StringReverse(char* psz)
{
char *p = psz;
char *q = psz + strlen(psz) - 1;
while (p < q)
{
char tmp = *p;
*p = *q;
*q = tmp;
p++;
q--;
}
return 1;

}

Thing is, when it gets to the *p = *q line, I get an access violation.



强烈表明要么指针值是错误的
或者你以无效方式使用该函数。
你的调试器说了什么?


A strong indication that either the pointer value is wrong
or you use the function in an invalid way.
What did your debugger say?



OP cou ldn''t *可能*做了类似的事情:

char * some_str =" reverse me" ;;
int result = StringReverse(some_str);
...



The OP couldn''t *possibly* done something like:

char * some_str = "reverse me";
int result = StringReverse(some_str);
...

Ya think? ;-)




是的。另一种可能性是他给这个函数喂了一个b样的C风格的字符串

(例如,没有终止的''\\'0'')


该函数有一个错误(1的减法是错误的),但只要他不给它一个空的char数组,它就不会崩溃。




-

Karl Heinz Buchegger
kb **** **@gascad.at


这篇关于另一个String逆转问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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