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.
The abbreviation char is used as a reserved keyword in some programming languages, such as C, C++, C#, and Java. It is short for character, which is a data type that holds one character (letter, number, etc.) of data. For example, the value of a char variable could be any one-character value, such as 'A', '4', or '#'.
char* is a pointer to a character. char is a character. A string is not a character. A string is a sequence of characters.
A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.
In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.
This is a char
:
char c = 't';
It can only hold one char
acter!
This is a C-string:
char s[] = "test";
It can hold multiple char
acters. 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.
The char type can only represent a single character. When you have a sequence of characters, they are piled next to each other in memory, and the location of the first character in that sequence is returned (assigned to test). Test is nothing more than a pointer to the memory location of the first character in "testing", saying that the type it points to is a char.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With