为什么(如何)我们可以使用char *来存储字符串? [英] Why(How) we can use char* to store a string?
本文介绍了为什么(如何)我们可以使用char *来存储字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
为什么这段代码有效?
Hi
Why this code works?
char* ch="hello";
cout<<ch;
我以为我们只能存储单个字符(char ch ='a'或'字符容器中的3'或'$')或ASCI编号(char ch = 65)。但是当我使用字符指针时,我可以在其中存储一个字符串。这怎么可能?还有其他我不知道的关于字符的内容吗?
I thought we can only store single characters (char ch='a' or '3' or '$') or ASCI numbers (char ch= 65) in a character container. But when I use a character pointer, I can store a string in it. How is this possible? And is there other things I don't know about chars?
推荐答案
')或字符容器中的ASCI编号(char ch = 65)。但是当我使用字符指针时,我可以在其中存储一个字符串。这怎么可能?还有其他我不了解的关于字符的事情吗?
') or ASCI numbers (char ch= 65) in a character container. But when I use a character pointer, I can store a string in it. How is this possible? And is there other things I don't know about chars?
你似乎不知道C / C ++中指针和字符串文字的基础知识。
I建议学习一些关于C / C ++的教程,例如 Character Sequences [ ^ ]和指针 [ ^ 一>]。或者甚至更好,浏览 C ++语言的所有部分[ ^ ]教程(或任何类似的参考资料)。
对于特定情况:
1.hello
是一个由6个字符组成的序列:h,e,l,l,o加上终止零字符。
2.字符序列可通过序列第一个字符的内存地址访问
3.该地址存储在指针变量中
4.在C / C ++中你写一个指向类型的指针为type * var = ...;
,例如char * s =hello;
5.将指向的指针命名为ch
愚弄所有人 - 将其命名为问候语
或类似
干杯>
Andi
You seem to not know the basics of pointers and string literals in C/C++.
I suggest to work through some tutorial on C/C++, e.g., Character Sequences[^] and Pointers[^]. Or even better, go through all parts of the C++ Language[^] tutorial (or any similar reference).
For the particular case:
1."hello"
is a sequence of 6 characters: h,e,l,l,o plus the terminating zero character.
2. that sequence of characters is accessible through the memory address of the first character of the sequence
3. that address is stored in a pointer variable
4. in C/C++ you write a pointer to a type astype * var = ...;
, e.g.char * s = "hello";
5. naming a pointer to character asch
is fooling everyone - name itgreeting
or alike
Cheers
Andi
char *
与char
不同。char *
表示指向包含字符串的存储位置的指针。
char只是存储字符值。
char *
is different fromchar
.char *
represents a pointer to a storage location that holds a string.
char is simply storing the character value.
这篇关于为什么(如何)我们可以使用char *来存储字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文