Newsletter Quiz Question Discussion - 4/6/2004
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Newsletter Quiz Question Discussion - 4/6/2004

  1. #1
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,423

    Newsletter Quiz Question Discussion - 4/6/2004

    This thread is to discuss the quiz question in the 4/6/2004 CodeGuru newsletter.

    Brad!
    -----------------------------------------------
    Brad! Jones, CodeGuru.com Site Director
    Developer.com Network Director / EiC

    webmaster@codeguru.com
    (My Latest Book)


    -----------------------------------------------

  2. #2
    Join Date
    Mar 2004
    Posts
    9

    Thumbs up pointer quiz

    these 3 are legal
    const char * myPtr = "A";
    char * const myPtr = "A";
    const char * const myPtr = "A";
    these 3 are NOT legal
    const char * myPtr = 'A';
    char * const myPtr = 'A';
    const char * const myPtr = 'A';
    reason: 'A' is a constant int.

  3. #3
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,423
    I will give you that all three are legal and correct.

    They are, however, each forcing something different.

    The single quotes should not be an issue as these are character pointer definitions (not strings).

    The differences between each of these three are..... part of the quiz question

    Brad!
    Last edited by Brad Jones; April 6th, 2004 at 04:42 PM.
    -----------------------------------------------
    Brad! Jones, CodeGuru.com Site Director
    Developer.com Network Director / EiC

    webmaster@codeguru.com
    (My Latest Book)


    -----------------------------------------------

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    well,

    1. const char * myPtr = "A";

    myPtr is not a const, but contents pointed to by myPtr is const

    2. char * const myPtr = "A";

    myPtr is const, but contents pointed by myPtr aren't

    3. const char * const myPtr = "A";

    Both myPtr and contents pointed to by myPtr are consts.

    One way to know is, as I read somewhere, draw an imaginary line thru *.
    If there is a const to it's left, the contents is const
    If there is a const to it's right, the var is a const

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    • const char * myPtr = "A";
      declares a pointer to a constant string. This means that you can later change myPtr to point to something different, but you cannot change the string's contents.
      I.e. ++myPtr is legal, but myPtr[0] = 'B' is illegal.
    • char * const myPtr = "A";
      Declares a constant pointer to a string. It's sort of the opposite from above. This means that you cannot change the value of myPtr, but you can change the contents of the string.
      I.e. ++myPtr is illegal, but myPtr[0] = 'B' is legal;
    • const char * const myPtr = "A";
      You can neither change the pointer nor the value pointed to.


    P.S. Is there a way to access the newsletter from the site?
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,423
    I'm going to try to start posting the newsletters to the site in the news and announcements section; however, this will generally be a few days after the newsletter goes out. (I will be posting last week's tomorrow....-- I'm behind).


    You can also get the newsletter by going to:

    http://e-newsletters.internet.com/codeguruupdate.html

    Brad!
    -----------------------------------------------
    Brad! Jones, CodeGuru.com Site Director
    Developer.com Network Director / EiC

    webmaster@codeguru.com
    (My Latest Book)


    -----------------------------------------------

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    I get the newletters by email already but I cannot access the email folder where it gets put from home, that's why I asked No big deal, sorry to be off-topic.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  8. #8
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211
    Once at these forums, there was a thread, where one member put a question, then the one who gives the correct answer, get his turn to ask any other question and in this way the thread moved on.

    Isn't it cool, if we have a thread like there. Like Brad asked first question and kirants answered correctly, now kirants asked and the one who'll answer correctly can ask the next question. (Just a suggestion )

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Brad Jones
    The differences between each of these three are..... part of the quiz question
    Without even looking at the newsletter yet...I would think that the following might be part of the answer...

    1. Reference - Can change the referenced object
    2. Const-Reference - Can not change the referenced object
    3. Const-Pointer - Can change the adress it is pointing to but not the object
    4. Pointer-Const - Can change the object but no the address it is pointing to
    5. Const-Pointer-Const - Can neither change the address nor the object
    Code:
    CFoo Instance;
    
    CFoo       &ReferenceToInstance      = Instance;       // 1.
    const CFoo &ConstReferenceToInstance = Instance;       // 2.
    const CFoo *pConstPointer            = &Instance;      // 3.
    CFoo       *const pPointerConst      = &Instance;      // 4.
    const CFoo *const pConstPointerConst = &Instance       // 5.
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center