-
November 22nd, 2011, 08:47 PM
#1
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.
-
November 22nd, 2011, 10:03 PM
#2
Re: Question about char pointer arrays
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.
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
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 24th, 2011, 12:26 PM
#3
Re: Question about char pointer arrays
Array[0] points the address of the first character of the string .
-
November 24th, 2011, 03:09 PM
#4
Re: Question about char pointer arrays
-
November 24th, 2011, 05:30 PM
#5
Re: Question about char pointer arrays
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.
-
November 24th, 2011, 06:33 PM
#6
Re: Question about char pointer arrays
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 24th, 2011, 06:59 PM
#7
Re: Question about char pointer arrays
Originally Posted by Eri523
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:
Thanks for the correction.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|