-1

I don't know why it is changing third sign to w, this is very weird notation(i know why it is third one, but I don't know how it works).

using namespace std;
char napis[] = "ALICE";

char& which(int n){
    return napis[n];
}

int main(){
which(2) = 'w';
cout << napis << endl;
return 0;
}
Yoda
  • 17,363
  • 67
  • 204
  • 344

2 Answers2

3

Get a book, seriously.

which() returns reference to third element of the array; by which(2) = ... you assign value to variable referenced by that reference.

But to understand how it really works you have to understand what a reference is - which is explained in that book you should get.

Griwes
  • 8,805
  • 2
  • 43
  • 70
1

Since the string "ALICE" is an array of chars, and an array starts at index 0, the 2nd index is the third char in the string.

You are also returning a reference instead of a copy of the char, this is why the string changes if you change it's value.

Melvin
  • 547
  • 2
  • 10