1

As far as my knowledge in c++, the & character can act as an address of operator(finding the address of a variable in memory) or as a bitwise AND operator or declaring references.

However if I run this code:

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

int main() {
    string s = "Stackoverflow";
    cout<<&s[0]<<endl<<&s[1]<<endl;
    return 0;
}

Output is

Stackoverflow
tackoverflow

I expected it to print the addresses of the first 2 characters of the string, however, I got the string itself starting from a different index. How does it work?

gitartha
  • 95
  • 9

0 Answers0