What does char * mean
So...say I had a function like this...
int function( const char *c )
{
//do 开发者_StackOverflow中文版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.
It means that this is a pointer to data of type char.
char *c
means that c is a pointer. The value that c points to is a character.
So you can say 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
.
Pointer to a char. That is, it holds the address at which a char is located.
Thats a pointer-to-char
. Now that you know this, you should read this:
About character pointers in 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. For example, http://boredzo.org/pointers/.
It means the argument should be a pointer to a character.
You would dereference it with *
as well.
You might want to read Const correctness page to get a good idea on pointer and const.
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.
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 c = 'a';
f( &c );
this passes the address of c
so that the function will be able to change the c
char.
char* str = "some string";
f( str );
This passes "some string" to f
, but f
cannot modify str
.
It's a really basic thing for c++, that higher-level languages (such as Java or Python) don't have.
精彩评论