C++ Rookie with first question.Using Code:Blocks 16.01 GNU GCC compiler. Thanks in advance. Code;
#include <iostream>
using namespace std;
int main(){
char charArr[]="Hello";
cout<<charArr<<endl; //outputs Hello.
string strArr[]={"Hello", "Stack", "overflow"};
string *pStrArr=strArr; //pointer to strArr; same as &strArr[0].
cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello
char charArr1[]="Hello";
char *pCharArr1=charArr1; /*pointer to charArr1.(charArr cout was Hello, not H, therefore i assumed we are storing in memory Hello);*/
cout<<*pCharArr1<<endl; /*dereferencing, outputs H, not Hello as i expected. */
return 0;
}
Observation; charArr outputs Hello, therefore i assumed that creating a pointer and dereferencing it should output Hello; Actual output is H, which seems inconsistent with the behavior observed on a string Array, whereas first element is both pointed and dereferenced.
Question is: Clearly i am failing to understand char Array. I would appreciate an explanation of the above in (as much as possible) layman terms.
PS:did use search function and talked to the Duck. Thanks for your time. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ After all the answers I realize the actual question should be why lines 2 and 4 produce different outputs, strArr being a memory address (behaves as a pointer) while the charArr outputs the array contents.
string strArr[]= {"hello","world","how", "are","you"};
cout<<strArr<<endl;//outputs 0x28fedc.
char charArr[]="Hello";
cout<<charArr<<endl; // outputs hello
Thanks