Const char* and Strings

I really do not understand how in the heavens does a char pointer be a string like "Hello" or something. Doesn't it have to be a reference to, well, a character?
for example :

char foo = 'b';

const char* bar = &b; //doesn't this make sense instead of :

const char* foobar = "Hello"; //this is where I start to die.



Also, I heard that arrays and pointers are somehow related. But do not get how a pointer is converted to string...Explain
Last edited on
Hi,

A string in it's simplest form is an array of characters, also known as a C string. Arrays are tightly connected with pointers. A pointer is a variable which holds a memory address, which is just a number - on a 64 bit system it will be 8 bytes. Array indexes for a C string are simply an offset from the address of the beginning of the array: 0 offset is the first char, 1 the second etc.

Here's the thing: arrays decay to their pointer address. Also a C style array of char is terminated with a null value '\0'. When the system is asked to print a string on the output with a function like printf (for C) or std::cout (for C++), the system prints the char at offset 0, followed by offset 1, until it reaches the null character, at which point it ends. Similar things happens when a string literal is used to initialise a char array, or some function is used to copy them etc.

Now std::string is a C++ class, which means an object created from the class can store data (the characters) and it has functions, algorithms and operators which can work with that data. For example, one can "add" strings to together with the + operator:

1
2
3
4
5
6
7
8
std::string h = "Hello ";
std::string w = "World!";
std::string Greeting = h + w ;

std::cout << Greeting << "\n" ;

// or
std::cout << h + w << "\n" ;


Note one can't do that with a C string, one must use a function to do that. Same for assigning one C string to another, can't just use the = operator, must use a function. But one can do that for std::string because it has overloads for various operators like =. std::string can also grow dynamically, where as a C string is a fixed sized array of char. So std::string is much more user friendly than a C string.

There are also algorithms, for example to find a character in a string. You can look at these in the reference section on this website.

The std::string class stores it's data dynamically on the heap, so there is no real need to use pointers or the new operator with it.

Good Luck !!

Edit :

In C++ you should avoid using arrays and pointers, use the STL (Standard Template Library) containers and their associated functions and algorithms. So try out std::vector and std::string instead of array of char and pointers. The reason for this is that STL things do their own memory management and have functionality built in to make things much easier and less error prone.
Last edited on
how in the heavens does a char pointer be a string

It doesn't. A pointer is not a string.

A pointer is an object that stores an address.
* That address my be nullptr; a clearly invalid address.
* It may be the address of a valid object
* It may be address of one object among a consecutive array of objects of same type.
* It may be invalid; address of memory that does not contain a valid object of said type.


A char pointer, if initialized so, can point to an element in an array of char elements. Consecutive characters do form text, do they not?
Last edited on
Topic archived. No new replies allowed.