Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c++ - What is a char*?

So I've been watching lectures from a University about C++, and I'm learning a lot, but one thing I still cannot understand is this:

Why do you have to do this sometimes?

char* test = "testing";

From what I've read/watched, I just don't understand why you have to put the *. From what I thought I understood, you only use * if you have an address, but maybe I'm just dead wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a char:

char c = 't';

It can only hold one character!


This is a C-string:

char s[] = "test";

It can hold multiple characters. Another way to write the above is:

char s[] = {'t', 'e', 's', 't', 0};

The 0 at the end is called the NUL terminator. It denotes the end of a C-string.


A char* stores the starting memory location of a C-string.1 For example, we can use it to refer to the same array s that we defined above. We do this by setting our char* to the memory location of the first element of s:

char* p = &(s[0]);

The & operator gives us the memory location of s[0]. Here is a shorter way to write the above:

char* p = s;

Notice:

*(p + 0) == 't'
*(p + 1) == 'e'
*(p + 2) == 's'
*(p + 3) == 't'
*(p + 4) == 0  // NUL

Or, alternatively:

p[0] == 't'
p[1] == 'e'
p[2] == 's'
p[3] == 't'
p[4] == 0  // NUL

Another common usage of char* is to refer to the memory location of a string literal:

const char* myStringLiteral = "test";

Warning: This string literal should not be changed at runtime. We use const to warn the programmer (and compiler) not to modify myStringLiteral in the following illegal manner:

myStringLiteral[0] = 'b';  // Illegal! Do not do this for const char*!

This is different from the array s above, which we are allowed to modify. This is because the string literal "test" is automatically copied into the array at initialization phase. But with myStringLiteral, no such copying occurs. (Where would we copy to, anyways? There's no array to hold our data... just a lonely char*!)


1 Technical note: char* merely stores a memory location to things of type char. It can certainly refer to just a single char. However, it is much more common to use char* to refer to C-strings, which are NUL-terminated character sequences, as shown above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...