C++ Char Array pointer behavior vs string Array pointer -
c++ rookie first question.using code:blocks 16.01 gnu gcc compiler. 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 strarr; same &strarr[0]. cout<<*pstrarr<<endl; //derreferencing pointer , outputs hello char chararr1[]="hello"; char *pchararr1=chararr1; /*pointer chararr1.(chararr cout hello, not h, therefore assumed storing in memory hello);*/ cout<<*pchararr1<<endl; /*dereferencing, outputs h, not hello expected. */ return 0; }
observation; chararr outputs hello, therefore assumed creating pointer , dereferencing should output hello; actual output h, seems inconsistent behavior observed on string array, whereas first element both pointed , dereferenced.
question is: failing understand char array. appreciate explanation of above in (as possible) layman terms.
ps:did use search function , talked duck. time. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ after answers realize actual question should why lines 2 , 4 produce different outputs, strarr being memory address (behaves pointer) while chararr outputs array contents.
string strarr[]= {"hello","world","how", "are","you"}; cout<<strarr<<endl;//outputs 0x28fedc. char chararr[]="hello"; cout<<chararr<<endl; // outputs hello
thanks
in code snippet
char chararr1[]="hello"; char *pchararr1=chararr1; /*pointer chararr1.(chararr cout hello, not h, therefore assumed storing in memory hello);*/ cout<<*pchararr1<<endl; /*dereferencing, outputs h, not hello expected. */
the object pchararr1
has type char *
. points first character of string "hello"
stored in array chararr1
. dereferencing pointer you'll object of type char
character pointed pointer. character 'h'
.
in code snippet
string strarr[]={"hello", "stack", "overflow"}; string *pstrarr=strarr; //pointer strarr; same &strarr[0]. cout<<*pstrarr<<endl; //derreferencing pointer , outputs hello
the object pstrarr
has type std::string *
, points object of type std::string
. dereferencing pointer you'll object of type std::string
contains character sequence "hello"
. in statement
cout<<*pstrarr<<endl; //derreferencing pointer , outputs hello
this sequence outputted.
thus in statement
cout<<chararr<<endl; //outputs hello
chararr
has type char *
(the array designator implicitly converted pointer first element).
in statement
cout<<*pchararr1<<endl; /*dereferencing, outputs h, not hello expected. */
*pchararr1
has type char
.
and @ last in statement
cout<<*pstrarr<<endl; //derreferencing pointer , outputs hello
*pstrarr
has type std::string
.
take account these declarations
char chararr1[]="hello"; char *pchararr1=chararr1; /*pointer chararr1.(chararr cout hello, not h, therefore assumed storing in memory hello);*/
the output of these statements
cout<<chararr1<<endl;
and
cout<<pchararr1<<endl;
will same , equal outputting string "hello"
however these statements
cout<<*chararr1<<endl;
and
cout<<*pchararr1<<endl;
only 1 character 'h'
outputted.
in first statement mentioned array designator implicitly converted pointer first element in expression *chararr1
Comments
Post a Comment