should the value of pointer always be an address?

Viewed 51

so ive just begun learning about pointer basics and ive come across something im stuck on. as the title says, should the value of the pointer must always be an address? because i saw a line of code, which says otherwise:

char *text  = "text";

this here is being used for the creation of a string, the other method is:

char text[] = "text"; 

which is pretty understandable.

could you guys explain to me what this line does exactly?

char *text  = "text";

a pointer is being used but what does it do and point to? how can you use it to then access the string created.

thanks.

1 Answers

"text" is a string literal. It is stored somewhere in memory and its address is used to initialise the pointer. You access the string as you would with any other pointer.

And as stated above

char *text  = "text";

is not legal C++ (it is legal C) the correct C++ is

const char *text  = "text";
Related