Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help me - C++

Help me - C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Coder11223344
    wrote on last edited by
    #1

    Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;

    CPalliniC L Z K 4 Replies Last reply
    0
    • C Coder11223344

      Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Did you try cdecl? const int * p; int const int * p; Diagrams are left as exercise...

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • C Coder11223344

        Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        const (C++) | Microsoft Docs[^]

        1 Reply Last reply
        0
        • C Coder11223344

          Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          So lazy you couldn't even put your homework into your own words? Just google it. But first, refer to the material your professor provided in class.

          Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

          K 1 Reply Last reply
          0
          • C Coder11223344

            Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;

            K Offline
            K Offline
            kalberts
            wrote on last edited by
            #5

            They are equivalent. The int value is constant, cannot be modified. Now that you decalre a pointer to this type, this pointer can be set to any constant int value, but the pointer itself may be moved around freely among several constant integer values. The difference comes when you move 'const' to the right of the asterisk: int *const ptr = &xxx; Now the xxx value may change, but ptr will always point to xxx. You are not allowed to move ptr to &yyy. You can consider *ptr as another way of writing xxx. Or ptr is another way of writing &xxx. You can't move xxx to refer to another variable either, so *ptr and xxx, or ptr and &xxx work the same way. You rarely need constant pointers; the address it is initialized with will serve the same purpose. But if the address expression is complex, and it is used many times, setting up a constant pointer may save both typing and improve readability (as long as a more descriptive name than "ptr" is chosen :-)).

            L 1 Reply Last reply
            0
            • Z ZurdoDev

              So lazy you couldn't even put your homework into your own words? Just google it. But first, refer to the material your professor provided in class.

              Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

              K Offline
              K Offline
              kalberts
              wrote on last edited by
              #6

              It sure may be homework, but then again: If different phrasings are both accepted by the compiler, it is legitimate to ask if they are identical, even if it is not homework :-). If "const int *ptr" is identical to "int const *ptr", is it then identical to "int *const ptr" as well? If you can shift the "const" keyword one position, why not two? There are several cases (in various languages) of modifiers that can be written in any order (when there are more than one). Knowing when order/position is significant and when it isn't (and which orders/positions are illegal) can be quite confusing until you have built expertise in the language!

              1 Reply Last reply
              0
              • K kalberts

                They are equivalent. The int value is constant, cannot be modified. Now that you decalre a pointer to this type, this pointer can be set to any constant int value, but the pointer itself may be moved around freely among several constant integer values. The difference comes when you move 'const' to the right of the asterisk: int *const ptr = &xxx; Now the xxx value may change, but ptr will always point to xxx. You are not allowed to move ptr to &yyy. You can consider *ptr as another way of writing xxx. Or ptr is another way of writing &xxx. You can't move xxx to refer to another variable either, so *ptr and xxx, or ptr and &xxx work the same way. You rarely need constant pointers; the address it is initialized with will serve the same purpose. But if the address expression is complex, and it is used many times, setting up a constant pointer may save both typing and improve readability (as long as a more descriptive name than "ptr" is chosen :-)).

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Member 7989122 wrote:

                They are equivalent.

                And nicely confusing. ;)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups