简体   繁体   English

为什么使用字符指针声明或定义字符串

[英]Why strings are declared or defined using character pointer

I dont know why we always declare like this 我不知道为什么我们总是这样声明

char* name="Srimanth"

instead of 代替

char name[]={"Srimanth"}

I am new to this things. 我是新来的东西。 so please be more specific while you are giving me an answer.. 因此,在给我答复时,请更加具体。

thank you. 谢谢。

String literal is a special, simple, form of writing an array aggregate: you can write "hello" instead of {'h', 'e', 'l', 'l', 'o', '\\0'} (note the terminating zero, which is added automatically). 字符串文字是写数组聚合的一种特殊,简单的形式:您可以写"hello"而不是{'h', 'e', 'l', 'l', 'o', '\\0'} (请注意终止零,它会自动添加)。

Note that an array declaration is not only possible, but is sometimes desirable: 请注意,数组声明不仅是可能的,而且有时是理想的:

char str[] = "hello";
str[0] = 'H'; // OK

lets you modify the string, as opposed to 允许您修改字符串,而不是

char *str = "hello";
str[0] = 'H'; // Undefined behavior

which does not allow modifications. 不允许修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM