简体   繁体   English

char *是什么意思

[英]What does char * mean

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? char *c是什么意思? I know about chars in general I think but I don't get what the * does...or how it changes the meaning. 我一般都知道chars,但我不知道*做了什么......或者它是如何改变意义的。

这意味着这是一个指向char类型数据的指针。

char *c means that c is a pointer. char *c表示c是指针。 The value that c points to is a character. 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. 另一方面,在这个例子中, const表示值c指向不能改变。 So you can say c = &a , but you cannot say *c = 'x' . 所以你可以说c = &a ,但你不能说*c = 'x' If you want a const pointer to a const character you would have to say const char* const c . 如果你想要一个指向const字符的const指针,你必须说const char* const c

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. 你可能想要读一下C中的指针,那里有大量的页面可以帮助你做到这一点。 For example, http://boredzo.org/pointers/ . 例如, http://boredzo.org/pointers/

Pointer to a char. 指向char的指针。 That is, it holds the address at which a char is located. 也就是说,它保存了char所在的地址。

Thats a pointer-to-char . 这是一个pointer-to-charpointer-to-char Now that you know this, you should read this: 既然你知道这一点,你应该读到这个:

About character pointers in C 关于C中的字符指针

It means the argument should be a pointer to a character. 这意味着参数应该是指向字符的指针

You would dereference it with * as well. 你也可以用*取消引用它。

您可能需要阅读Const正确性页面以获得关于指针和const的好主意。

http://cslibrary.stanford.edu/ is the best resource that I have come across to learn about pointers in C . http://cslibrary.stanford.edu/是我学习C中指针的最佳资源。 Read all the pointer related pdfs and also watch the binky pointer video. 阅读所有与指针相关的pdf,并观看binky指针视频。

This is a pointer to a char type. 这是一个指向char类型的指针。 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. 例如,此函数可以获取char的地址并修改char或指向字符串的指针的副本。 Here's what I mean: 这就是我的意思:

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。

char* str = "some string";
f( str );

This passes "some string" to f , but f cannot modify str . 这会将“some string”传递给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)没有。

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

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