char* is a pointer to a char. A char holds a single character, such as 'A'. A char* holds the memory address of a char instead, so it points to where a character lives. The star marks the variable as a pointer, and the char part says what kind of thing it points to.
Declaring a char pointer
char letter = 'A';
char* ptr = &letter; // ptr stores the address of letter
The & here takes the address of letter. The pointer doesn’t copy the character. It refers to the original.
Two style notes. char* ptr and char *ptr mean the same thing, so pick one and stay consistent. Also, the star applies to one name at a time. In char* a, b;, a is a char* but b is a plain char.
Reading and writing through the pointer
#include <iostream>
int main() {
char letter = 'A';
char* ptr = &letter;
std::cout << *ptr << "\n"; // prints A
*ptr = 'B'; // changes letter to B
std::cout << letter << "\n"; // prints B
}
The * in front of ptr is the dereference operator. It means “the character at this address.” Because *ptr refers to a real place, you can assign to it, which is why *ptr = 'B'; works. The idea behind this is covered in What Does lvalue Mean?
Strings and char pointers
In C, and in C++ code that works with C-style strings, text is stored as an array of char that ends with a zero character, written '\0'. A char* can point to the first character of that array. Code moves along the array until it reaches the zero.
const char* greeting = "Hello";
std::cout << greeting[0] << "\n"; // prints H
Note the const. A string literal such as "Hello" is read-only, so const char* is the accurate type. Changing a character through it won’t compile:
greeting[0] = 'h'; // error: the characters are const
That’s the same kind of problem covered in Fixing the Expression Must Be a Modifiable Lvalue Error in C++.
Older code sometimes writes char* name = "Hello"; without const. Compilers usually warn about this, because a string literal isn’t meant to be changed. Use const char* when you only read the text, and a writable array when you need to change it:
char name[] = "Hello"; // writable copy of the text
name[0] = 'h';
Null and uninitialized pointers
A char* that hasn’t been given an address points to an unknown place. Reading or writing through it with * is undefined behavior, and it often crashes the program. Crashes like that are often reported as a segmentation fault. For what that means, see What Is a Segmentation Fault in C++?
To avoid this, set a pointer to nullptr when it doesn’t point to anything yet, and check it before you use it:
char* ptr = nullptr; // points to nothing yet
if (ptr != nullptr) {
*ptr = 'x'; // runs only when ptr has an address
}
A pointer set to nullptr is a null pointer. What Is a Null Pointer in C? covers that idea in more depth.
Arrays and char pointers
An array of char and a char* are related but not the same.
char word[] = "Hi"; // an array that holds its own copy: 'H', 'i', '\0'
char* p = word; // a pointer to the first element of word
The array owns its storage, and the pointer only refers to it. sizeof(word) gives the size of the whole array, while sizeof(p) gives the size of the pointer itself. Are char[] and char * the same in C? goes deeper into the C side of this question.
When to use char* in modern C++
For storing and changing text in modern C++, std::string is usually easier and safer. It manages its own memory and knows its own length. Use char* mainly when you’re calling C functions or working with raw buffers that a library hands you.
Summary
char* is a variable that stores the address of a char. The * reads or writes the character at that address. It shows up in C-style strings and low-level code, so check for nullptr before using one, and use const char* for string literals.