1

I wrote the below code for my understanding of pointers.

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
    string a = "hello";
    const char *st = &a[0];
    printf("%p\n", &st[0]);
    printf("%p\n", &(a[0]));
    printf("%c\n", *st);
    printf("%p", &a);
}

This is the output that I get

0x1265028
0x1265028
h
0x7ffe26a91c40

If my understanding is correct &a should return the address of string, why is the value returned by &a different than the rest ?

phuclv
  • 37,963
  • 15
  • 156
  • 475
babybrain
  • 95
  • 5
  • 8
    Assuming that is `std::string` then you are taking the address of the object - which is not the same as the address of the memory it manages – UnholySheep Aug 31 '22 at 14:15

3 Answers3

13

A std::string is a C++ object, which internally holds a pointer to an array of chars.

In your code, st is a pointer to the first char in that internal array, while &a is a pointer to the C++ object. They are different things, and therefore the pointer values are also different.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • For plus 8 you might want to rephrase "which internally holds a pointer to an array of chars." to cover the short string optimisation possibility. – Bathsheba Aug 31 '22 at 15:18
  • @Bathsheba I think SSO is a private implementation detail that doesn't change the the externally-visible behavior of the string nor the validity of the answer as given... but if someone wants to read about SSO, here's a link that discusses it better than I can here: https://stackoverflow.com/questions/10315041/meaning-of-acronym-sso-in-the-context-of-stdstring/10319672#10319672 – Jeremy Friesner Aug 31 '22 at 17:24
  • It's just that is doesn't *always* hold a pointer to an array of chars. Personally I think it's important to be precise, or vague (something like "internally can hold a pointer to...") in your case. Else readers start believing in absolutes. – Bathsheba Aug 31 '22 at 17:26
  • I'd say that even when SSO in is use, the `std::string` has a pointer to an array of chars; it's just that it's deriving that pointer from its this-pointer when necessary, rather than keeping it as a separate member-variable. But this is all very "inside baseball" and probably more confusing than helpful to someone who is just learning C++. – Jeremy Friesner Aug 31 '22 at 17:29
6

&a is the address of the variable a of type std::string. Since std::string contains a character array of variable length, it must use dynamic allocation and store the real array somewhere else.

However, std::string has many operator overloads. a[0] returns a reference to the first character in a's array, and &a[0] is the address of that character. That's why &st[0] and &a[0] would be the same address, as st also points to the first character in a's array.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
phuclv
  • 37,963
  • 15
  • 156
  • 475
4

0x1265028 - address of first char
0x1265028 - address of first char
h - first char value
0x7ffe26a91c40 - address of std::string object