我的虚拟代码出了什么问题? [英] what's wrong with my dummy code?
问题描述
你好!
我刚写了一个反转字符串的函数。对我来说似乎都没关系,
但程序会在* s = * t_str上进行分段错误。
这里是代码:
#include< stdio.h>
#include< stdlib.h>
void reverse(char * s)< br $> b $ b $
char * t_str;
for(t_str = s; * t_str!=''\'''; t_str ++);
t_str--;
for(; s< t_str; s ++,t_str- - )$
t = * s;
* s = * t_str;
* t_str = t;
}
}
int main(int argc,char * argv [])
{
char * s =" Hello world!" ;;
reverse(s);
printf("%s",s);
返回0;
}
出了什么问题?!
谢谢!< br $>
-
Shya,
hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Here''s the code:
#include <stdio.h>
#include <stdlib.h>
void reverse(char *s)
{
char t;
char* t_str;
for (t_str = s; *t_str != ''\0''; t_str++);
t_str--;
for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}
int main(int argc, char *argv[])
{
char* s = "Hello world!";
reverse(s);
printf("%s", s);
return 0;
}
what''s wrong??!
thanks!
--
Shya,
推荐答案
shya写道:
[...]出了什么问题?!
[...] what''s wrong??!
http:/ /www.eskimo.com/~scs/C-faq/q1.32.html
-
post tenebras lux。 post fenestras tux。
http://www.eskimo.com/~scs/C-faq/q1.32.html
--
post tenebras lux. post fenestras tux.
shya写道:
char * s =" Hello world!";
char* s = "Hello world!";
char s [] =" Hello world!" ;;
就行了。
char s[] = "Hello world!";
will be OK.
shya写道:
shya wrote:
嗨那里!
我刚写了一个反转字符串的函数。对我来说似乎一切都好,
但程序在* s = * t_str上继续分段错误。
这里是代码:
#include< stdio。 h>
#include< stdlib.h>
void reverse(char * s)
{
char t;
char * t_str; <对于(; s< t> t_str; s ++,t_str--){
t = * s;
* s = * t_str;
* t_str = t;
}
}
int main(int argc,char * argv [])
{
char * s =" Hello world!" ;;
reverse(s);
printf("%s",s);
返回0;
}
有什么问题?!谢谢!
-
Shya,
hi there!
I just wrote a function that reverse a string. It seems all ok to me,
but the program goes on segmentation fault on *s = *t_str.
Here''s the code:
#include <stdio.h>
#include <stdlib.h>
void reverse(char *s)
{
char t;
char* t_str;
for (t_str = s; *t_str != ''\0''; t_str++);
t_str--;
for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}
int main(int argc, char *argv[])
{
char* s = "Hello world!";
reverse(s);
printf("%s", s);
return 0;
}
what''s wrong??!
thanks!
--
Shya,
PS:改变函数的参数不是一个好习惯。
PS: Changing the arguments of a function isn''t a good habit.
这篇关于我的虚拟代码出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!