0

I'm a total beginner in C++.

I'm not sure if I write the title properly.

I'd like to ask you guys about the line I marked.

#include <iostream>

using namespace std;

int main()
{
char str1[] = "abcd"
char*str2 = str1;

cout << str1 << endl;
cout << str2 << endl; // <- I don't get it.

return 0;
}

In the line I marked, the outcome is "abcd". But I don't understand why it is "abcd".

Cos I thought str2 has str1's memory address. Shouldnt it show the address instead of the value of str1?

Also, when I put cout << *str2 << endl; in that line,

it only shows "a". Why does it show the value of str1[0]? I know the array type's address is array[0]'s address.

thx!

Chick
  • 1
  • Does this answer your question? [cout << with char\* argument prints string, not pointer value](https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value) – TruthSeeker Mar 30 '22 at 13:28
  • similar to https://stackoverflow.com/q/17813423/4139593 – TruthSeeker Mar 30 '22 at 13:29

0 Answers0