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. code analysis

code analysis

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 3 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.
  • M Offline
    M Offline
    Member_15070286
    wrote on last edited by
    #1

    Hi, #include int main() { char str[20] = "Hello"; char *const p=str; *p='M'; printf("%s\n", str); return 0; } output: Mello As per my knowledge when we use const then we can't change a char in t then how output is Mello instead of Hello.

    L Greg UtasG 2 Replies Last reply
    0
    • M Member_15070286

      Hi, #include int main() { char str[20] = "Hello"; char *const p=str; *p='M'; printf("%s\n", str); return 0; } output: Mello As per my knowledge when we use const then we can't change a char in t then how output is Mello instead of Hello.

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

      You need to read the declaration right to left:

      char *const p=str;
      | | | |
      | | | +- the variable p
      | | +----- is a constant
      | +---------pointer
      +------------to a character

      So const here applies to the pointer p and not to the data that it is pointing to. That would be:

      const char* p=str; // p is a pointer to a character constant.

      M 1 Reply Last reply
      0
      • M Member_15070286

        Hi, #include int main() { char str[20] = "Hello"; char *const p=str; *p='M'; printf("%s\n", str); return 0; } output: Mello As per my knowledge when we use const then we can't change a char in t then how output is Mello instead of Hello.

        Greg UtasG Offline
        Greg UtasG Offline
        Greg Utas
        wrote on last edited by
        #3

        const applies to what's on its immediate left: a pointer (*) or a type (char and many others, even ones that you've defined). If there's nothing on its left--that is, when it appears first--const applies to what's on its immediate right.

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

        <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
        <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

        M 1 Reply Last reply
        0
        • L Lost User

          You need to read the declaration right to left:

          char *const p=str;
          | | | |
          | | | +- the variable p
          | | +----- is a constant
          | +---------pointer
          +------------to a character

          So const here applies to the pointer p and not to the data that it is pointing to. That would be:

          const char* p=str; // p is a pointer to a character constant.

          M Offline
          M Offline
          Member_15070286
          wrote on last edited by
          #4

          Hi Richard, Thank you for your reply. I understood the concept clearly. Best Regards Niharika

          1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            const applies to what's on its immediate left: a pointer (*) or a type (char and many others, even ones that you've defined). If there's nothing on its left--that is, when it appears first--const applies to what's on its immediate right.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            M Offline
            M Offline
            Member_15070286
            wrote on last edited by
            #5

            Hi Greg, Thank you for your immediate response. I understood the point clearly. Regarda, Niharika

            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