CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    40

    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.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Question about char pointer arrays

    Quote Originally Posted by AwArEnEsS View Post
    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.

  3. #3
    Join Date
    Nov 2011
    Posts
    6

    Re: Question about char pointer arrays

    Array[0] points the address of the first character of the string .

  4. #4
    Join Date
    Oct 2009
    Posts
    40

    Re: Question about char pointer arrays

    Thanks for your answers.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Question about char pointer arrays

    Quote Originally Posted by bluecoder View Post
    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.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Question about char pointer arrays

    Quote Originally Posted by GCDEF View Post
    Array[0] is the first character of the string. [...]
    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:

    Code:
    char* Array[5];
    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.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Question about char pointer arrays

    Quote Originally Posted by Eri523 View Post
    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:

    Code:
    char* Array[5];
    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
  •  





Click Here to Expand Forum to Full Width

Featured