2

I have been teaching myself C and C++ and am somewhat familiar with pointers. My main question is Does const char* actually store a memory address? This question arose when working with strings. How can you assign a string to what is supposed to hold a memory address?

#include <iostream>

int main(void) {
    const char* x = "Hello";
    std::cout << x << std::endl; // Returns Hello
    std::cout << &x << std::endl; // Returns the actual address
}

To my knowledge the above code should print a memory address, so what actually is a char*?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Aayaan Sahu
  • 125
  • 1
  • 8
  • 5
    I would strongly suggest not trying to learn both languages simultaneously. They are different languages, but are also sufficiently similar that you will end up confusing yourself more than necessary. – cigien Jan 01 '21 at 03:10
  • A pointer type can be initialized with an array. Both in C and C++. String literal `"Hello"` is an array of characters. Your pointer variable `x` points at the first array element. Also, in C++, we do not need to use `void` to signal that the function accepts no parameters. A simple `int main() {}` signature will do. – Ron Jan 01 '21 at 03:16
  • 2
    `x` contains the address of the first character of the string literal `"Hello"`. The `operator<<()` for output streams has multiple overloads. One accepts a `const char *` argument which will output characters at that address until it reaches the nul character (printing `Hello`). Another overload accepts a `void *` argument and outputs (a representation of) the actual address. If you do `std::cout << (void *)x` it will call the `operator<<()` overload that accepts a `void *`, and therefore print the address contained in `x` (which is a different value from `&x`). – Peter Jan 01 '21 at 03:36

2 Answers2

3

char* does in fact store an address. When you assign a constant string, C++ puts that constant string somewhere convenient (probably in the .DATA section of the executable) and stores a pointer to it in the variable x. In particular, this is one of the reasons a constant character string is, well, const. The string of text that the const char* points to may be used in other places in the code.

Note that cout has special behavior when printing character arrays. When you do &x, you're actually getting the address of the local variable, i.e. a thing of type const char**. If you want the actual address of the string, you can use (void*)x to suppress the usual string printing behavior.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • 3
    Worth mentioning is that the [string literal](https://en.cppreference.com/w/cpp/language/string_literal) such as `"Hello"` has a _static storage duration_. `.DATA` section is an implementation/hardware specific detail. – Ron Jan 01 '21 at 03:32
1

const char* indeed holds a memory address, not the string. In order for this to work, the compiler needs to perform some "magic": it places the content of the string literal at some address in the memory, and then sets that address into your const char* pointer. Therefore, the pointer stores the memory address of the initial character of your string.

One of the consequences of this is that you cannot get the length of your string directly from the const char* pointer: you need to go through a function to get the length.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523