C++ char * 是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5710703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does char * mean
提问by Hymaneown
So...say I had a function like this...
所以......说我有一个这样的功能......
int function( const char *c )
{
//do something with the char *c here...
}
what does char *c
mean? I know about chars in general I think but I don't get what the * does...or how it changes the meaning.
是什么char *c
意思?我大致了解字符,但我不明白 * 的作用......或者它如何改变含义。
回答by Todd Hopkinson
It means that this is a pointer to data of type char.
这意味着这是一个指向 char 类型数据的指针。
回答by spockaroo
char *c
means that c is a pointer. The value that c points to is a character.
char *c
意味着 c 是一个指针。c 指向的值是一个字符。
So you can say char a = *c
.
所以你可以说char a = *c
。
const
on the other hand in this example says that the value c points to cannot be changed.
So you can say c = &a
, but you cannot say *c = 'x'
. If you want a const pointer to a const character you would have to say const char* const c
.
const
另一方面,在这个例子中,c 指向的值不能改变。所以你可以说c = &a
,但你不能说*c = 'x'
。如果您想要一个指向 const 字符的 const 指针,则必须说const char* const c
.
回答by sjr
This is a pointer to a character. You might want to read up about pointers in C, there are about a bazillion pages out there to help you do that. For example, http://boredzo.org/pointers/.
这是一个指向字符的指针。您可能想阅读 C 中的指针,大约有无数的页面可以帮助您做到这一点。例如,http://boredzo.org/pointers/。
回答by splonk
Pointer to a char. That is, it holds the address at which a char is located.
指向字符的指针。也就是说,它保存了一个字符所在的地址。
回答by Sadique
Thats a pointer-to-char
. Now that you know this, you should read this:
那是一个pointer-to-char
。既然你知道了这一点,你应该阅读以下内容:
回答by alex
回答by adatapost
You might want to read Const correctnesspage to get a good idea on pointer and const.
您可能想阅读Const 正确性页面以了解有关指针和常量的好主意。
回答by deovrat singh
http://cslibrary.stanford.edu/is the best resource that I have come across to learn about pointers in C . Read all the pointer related pdfs and also watch the binky pointer video.
http://cslibrary.stanford.edu/是我在 C 中学习指针的最佳资源。阅读所有与指针相关的 pdf 并观看 binky 指针视频。
回答by Pooh
This is a pointer to a char
type. For example, this function can take the address of a char and modify the char, or a copy of a pointer, which points to an string. Here's what I mean:
这是一个指向char
类型的指针。例如,此函数可以获取一个字符的地址并修改该字符或指向字符串的指针的副本。这就是我的意思:
char c = 'a';
f( &c );
this passes the address of c
so that the function will be able to change the c
char.
这传递了 的地址,c
以便函数能够更改c
字符。
char* str = "some string";
f( str );
This passes "some string" to f
, but f
cannot modify str
.
这会将“某个字符串”传递给f
,但f
不能修改str
。
It's a really basic thing for c++, that higher-level languages (such as Java or Python) don't have.
对于 C++ 来说,这是一个非常基本的东西,高级语言(例如 Java 或 Python)没有。