0

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.

Jason
  • 36,170
  • 5
  • 26
  • 60
abdullah
  • 9
  • 5
  • Refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There are plenty of related SO posts for this. The `"text"` is a string literal that decays to `const char*`. Then that `const char*` is used to initialize the `text`. See [Initializing a char pointer C++](https://stackoverflow.com/questions/45457291/initializing-a-char-pointer-c) – Jason Sep 23 '22 at 08:00
  • 1
    `char *text = "text";` is not valid c++. See [Why can a char pointer variable be initialized to a string](https://stackoverflow.com/questions/43564633/why-can-a-char-pointer-variable-be-initialized-to-a-string-but-an-int-pointer-va) – Jason Sep 23 '22 at 08:01
  • Here's a hint, `char *text = "text";` is usually a pointer to read-only, global memory, where `text[0] = 'X'` is undefined behavior. Whereas `char text[] = "text";` is mutable memory on the stack(or global scope if it is global). – Dmytro Sep 23 '22 at 08:02
  • Both of your questions are explained here: [initializing char pointer as string vs other type pointers as arrays](https://stackoverflow.com/a/14295273/12002570) – Jason Sep 23 '22 at 08:07
  • https://en.cppreference.com/w/cpp/language/string_literal – 463035818_is_not_an_ai Sep 23 '22 at 08:08
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. – Jason Sep 23 '22 at 08:09
  • @JasonLiam you are being a bit aggressive. Its a new user and you are bombarding them with links and what not, just to point out that they shouldnt have asked the question. Not everybody is as good with finding questions and answers. Especially when not knowing what terms to look for I can understand why this question was posted. Note that OP seems to be not even aware of the term "string literal". They could be, and they should be if they read their books carefully, but well, they didnt – 463035818_is_not_an_ai Sep 23 '22 at 08:12
  • @463035818_is_not_a_number This is exactly why **C++ must be learnt by using a good c++ book** and not by searching online articles to read about it. I've just provided relevant information that will help OP in the long run. My first comment at the top mentions the books that can be used. Then my next comment says why the shown code is invalid. Finally my third comment mentions the SO guidelines. – Jason Sep 23 '22 at 08:18

1 Answers1

0

"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";
john
  • 85,011
  • 4
  • 57
  • 81
  • There are plenty and plenty of dupes for this. For example, [Initializing a char pointer C++](https://stackoverflow.com/questions/45457291/initializing-a-char-pointer-c) – Jason Sep 23 '22 at 08:03
  • 1
    "in memory and its address is used to initialise the pointer" nitpick: `text` is pointer to the first element not to the array. its the same value but different type – 463035818_is_not_an_ai Sep 23 '22 at 08:07