Question about char pointer arrays
I have a question regarding to char pointer arrays.
char* S="Just a try!";
char* Array[5];
Array[0]=S;
cout << Array[0];
This code prints the text "Just a try!".Does Array[0] point to the first character J's address in memory or point to the character J?If Array[0] points to the first character J's address in memory,then why don't we use *(Array[0]) instead of just writing Array[0]?When we type cout << Array[0] why doesn't it write the address of Array[0]?
I am a bit confused about pointers like you see.
Re: Question about char pointer arrays
Quote:
Originally Posted by
AwArEnEsS
Does Array[0] point to the first character J's address in memory or point to the character J?
Since, naturally, the character 'J' resides at its own address in memory, both sides of your "or" are true, IOW it doesn't matter because essentially it means the same.
Quote:
If Array[0] points to the first character J's address in memory,then why don't we use *(Array[0]) instead of just writing Array[0]?When we type cout << Array[0] why doesn't it write the address of Array[0]?
There's a subtle difference between the two: While Array[0] represents (a pointer to) an array of characters, *(Array[0]) represents a single, isolated character, namely the first one in that array.
What cout << actually does output, though, solely depends on the the operator<<() implementations that take a right-hand operand of type char * or char, respectively. It would be absolutely no problem to implement the operator taking a char * so that it just outputs the first character in the array pointed to, just like the char variety does, it's just that the stahdard mandates otherwise.
HTH
Re: Question about char pointer arrays
Array[0] points the address of the first character of the string .
Re: Question about char pointer arrays
Re: Question about char pointer arrays
Quote:
Originally Posted by
bluecoder
Array[0] points the address of the first character of the string .
Array[0] is the first character of the string. It doesn't point to anything, address or otherwise. Array points to to the first character, Array[0] is the first character.
Re: Question about char pointer arrays
Quote:
Originally Posted by
GCDEF
Array[0] is the first character of the string. [...]
:D Actually, I was on the verge of succumbing to the temptation of replying essentially the same to bluecoder's post. However, the OP's Array is declared as:
;) :p :rolleyes:
Re: Question about char pointer arrays
Quote:
Originally Posted by
Eri523
:D Actually, I was on the verge of succumbing to the temptation of replying essentially the same to
bluecoder's post. However, the OP's
Array is declared as:
;) :p :rolleyes:
Thanks for the correction.