字符串中的字符 [英] Char in String

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

问题描述




任何人都可以告诉我为什么程序1有效,程序2没有

工作?谢谢!


program1


#include< iostream>


using namespace std;


main()

{


char * x =" abcdefg" ;;


x [1] =''1'';


cout<< x;


返回0; < br $>

}


计划2

#include< iostream>


使用命名空间std;


main()

{


char * x = " abcdefg";


x [1] =''1'';


cout<< x;


返回0;


}

谢谢,

Michael

解决方案

Michael写道:


任何人都可以告诉我为什么程序1有效,程序2没有

有效吗?谢谢!



我不能说它们之间有任何区别。但它们都是错的。


#include< iostream>

使用命名空间std;

main ()



main应返回int。


{

char * x =" abcdefg";

x [1] =''1'';

cout<< x;

返回0;

}


program 2


#include< iostream>

使用命名空间std;

main()

{

char * x =" abcdefg" ;;

x [1] =''1'';

cout<< x;

返回0;

}



程序创建未定义的行为使用

语言中的愚蠢漏洞。字符串文字abcdefg具有char const []类型。但是文字

可以默默地转换为char *。该规则支持遗留代码

处理字符串文字很差。


但是,字面值仍然是常量,因此写入它是未定义的。

程序可以执行任何操作,包括似乎正常工作。几乎任何事情都可以改变



-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!! !


Michael写道:





可以有人请告诉我为什么程序1工作而程序2不工作?b $ b工作?谢谢!



我两者之间没有任何区别。


program1


#include< iostream>


使用命名空间std;


main()



您忘了指定main的返回类型。编译器不应该接受这个。


{


char * x =" ABCDEFG英寸;



糟糕的风格。你让指向非const的指针指向常量数据。


x [1] =''1'';



在这里,您尝试修改常量数据。这会导致未定义的行为。


cout<< x;


返回0;

}


Michael发布:


program1


#include< iostream>


使用命名空间std;


main()



选择以下选项之一:


int main()

int main(无效)

签署main()

签署main(无效)

int signed main()

int signed main(void)

signed int main()

signed int main(void)

你会发现超过90%的C ++程序员使用:


int main()


{


char * x = " ABCDEFG英寸;



char'的数组,'abcdefg"'是const,即使C ++类型系统

也不是反映这一点。在可能的情况下,您应该更喜欢将其

地址存储在指向const的指针中:


char const * x =" abcdefg" ;;


x [1] =''1'';



你正在改变一个const对象 - 未定义的行为。


program 2



< snip Program 2>

我没有使用细齿梳子,但似乎程序1和程序2的

代码完全相同。


-


Frederick Gotham


Hi,

Could anyone please tell me why program 1 works and program 2 does not
work? Thanks!

program1

#include <iostream>

using namespace std;

main()
{

char* x="abcdefg";

x[1]=''1'';

cout<<x;

return 0;

}

program 2

#include <iostream>

using namespace std;

main()
{

char* x="abcdefg";

x[1]=''1'';

cout<<x;

return 0;

}
Thanks,
Michael

解决方案

Michael wrote:

Could anyone please tell me why program 1 works and program 2 does not
work? Thanks!

I can''t tell any difference between them. They are both wrong, though.

#include <iostream>
using namespace std;
main()

main should return int.

{
char* x="abcdefg";
x[1]=''1'';
cout<<x;
return 0;
}

program 2

#include <iostream>
using namespace std;
main()
{
char* x="abcdefg";
x[1]=''1'';
cout<<x;
return 0;
}

The program creates "undefined behavior" using a silly loophole in the
language. A string literal, "abcdefg", has type char const []. But a literal
can silently convert to a char *. That rule supports legacy code that
treated string literals poorly.

However, the literal is still constant, so writing on it is undefined. The
program may do anything, including appear to work correctly. And it can
change when nearly anything happens.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Michael wrote:

Hi,

Could anyone please tell me why program 1 works and program 2 does not
work? Thanks!

I don''t se any difference between the two.

program1

#include <iostream>

using namespace std;

main()

You forgot to specify the return type for main. The compiler should not
accept this.

{

char* x="abcdefg";

Bad style. You let a pointer to non-const point to constant data.

x[1]=''1'';

Here, you try to modify constant data. This results in undefined behavior.

cout<<x;

return 0;

}


Michael posted:

program1

#include <iostream>

using namespace std;

main()


Choose one of the following:

int main()
int main(void)
signed main()
signed main(void)
int signed main()
int signed main(void)
signed int main()
signed int main(void)
You''ll find that upwards of 90% of C++ programmers use:

int main()

{

char* x="abcdefg";


The array of char''s, "abcdefg", is const, even though the C++ type system
doesn''t reflect this. Where possible, you should prefer to store its
address in a pointer to const:

char const *x = "abcdefg";

x[1]=''1'';


You''re altering a const object -- Undefined Behaviour.

program 2

<snip Program 2>
I haven''t gone through it with a fine-tooth comb, but it appears that the
code for Program 1 and Program 2 are identical.

--

Frederick Gotham


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

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