-4

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

  • You seem aware that `char *pCharArr1=charArr1;` makes `pCharArr1` equal to `&charArr1[0]`. Therefore, `*pCharArr1` is equivalent to `charArr1[0]` - a single character. If you print `cout << charArr1[0]`, you'll also see a single character `H`. I'm not sure why you are surprised by the behavior. – Igor Tandetnik Sep 10 '17 at 23:44
  • Don't use frickin' raw arrays! _"PS:did use search function and talked to the Duck. Thanks for your time. "_ Talk to your duck further! – user0042 Sep 10 '17 at 23:48
  • You need a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Passer By Sep 11 '17 at 00:38
  • This is the wrong duck: https://www.youtube.com/watch?v=32VLnTKz0CI . If you can understand what's he's saying, ignore it. – user4581301 Sep 11 '17 at 02:01

1 Answers1

1

In this code snippet

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. */

the object pCharArr1 has type char *. It points to the first character of the string "Hello" stored in the array charArr1. Dereferencing the pointer you'll get an object of the type char that is the character pointed to by the pointer. It is the character 'H'.

In this code snippet

string strArr[]={"Hello", "Stack", "overflow"};
string *pStrArr=strArr; //pointer to strArr; same as &strArr[0].
cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

the object pStrArr has type std::string * and points to an object of the type std::string. Dereferencing the pointer you'll get an object of the type std::string that contains the character sequence "Hello". So in this statement

cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

this sequence is outputted.

Thus in this statement

cout<<charArr<<endl;  //outputs Hello

charArr has type char * (the array designator is implicitly converted to pointer to its first element).

In this statement

cout<<*pCharArr1<<endl;  /*dereferencing, outputs H, not Hello as i expected. */

*pCharArr1 has type char.

And at last in this statement

cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

*pStrArr has type std::string.

Take into account that for these declarations

char charArr1[]="Hello";
char *pCharArr1=charArr1;  /*pointer to charArr1.(charArr cout was Hello, not H, therefore i assumed we are storing in memory Hello);*/

the output of these statements

cout<<CharArr1<<endl;  

and

cout<<pCharArr1<<endl;  

will be the same and will be equal to outputting the string "Hello"

However for these statements

cout<<*CharArr1<<endl;  

and

cout<<*pCharArr1<<endl;  

only one character 'H' will be outputted.

In the first statement as it was mentioned the array designator is implicitly converted to pointer to its first element in the expression *CharArr1

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Makes sense Vlad & Igor, so looking at char charArr[]="Hello"; as the equivalent char charArr[]={'H','e','l','l','o'} i am actually pointing at the FIRST char, which is 'H'...i guess what I am struggling to understand (and to formulate as an inteligible question) is why cout< – RookieCplusplus Sep 11 '17 at 00:20
  • @RookieCplusplus This declaration charArr[]="Hello"; is equivalent to the following declaration charArr[]= { 'H', 'e', 'l', 'l', 'o', '\0' };; – Vlad from Moscow Sep 11 '17 at 00:24
  • @RookieCplusplus Each statement with the << operator outputs the object that specified in the right side. If the type of the object is char then a character is outputted. If the type of the object is std::string then the string stored in the object is outputted. – Vlad from Moscow Sep 11 '17 at 20:43