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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can be p[-1] dereferenced?

can be p[-1] dereferenced?

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • S Offline
    S Offline
    sakthimuhuu
    wrote on last edited by
    #1

    I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi

    _ J CPalliniC S 4 Replies Last reply
    0
    • S sakthimuhuu

      I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      The statement is perfectly valid "in this case". Using an index will simply add it to the address. So p[-1] will internally become *(p-1). In this case, since you're incrementing p twice, p[-1] should print e. Correction - My earlier comment would be true if you change the statement to printf("%c", p[-1]);

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      1 Reply Last reply
      0
      • S sakthimuhuu

        I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        p[-1] returns the character at that position ('e' in your case). When passing this value as address of a string to printf, it will try to print the memory content at the address 0x65 as string.

        1 Reply Last reply
        0
        • S sakthimuhuu

          I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi

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

          While p[-1] is valid (why didn't you try to compile it?), you are using it the wrong way: a char pointer (char *) is expected by the format specifier "%s" while you are passing a char ( p[-1], is like *(p-1), that is a char). You should have written either:

          printf("%s\n", &p[-1]);

          producing output

          est

          or

          printf("%c\n",p[-1]);

          , producing output

          e

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

          In testa che avete, signor di Ceprano?

          S 1 Reply Last reply
          0
          • CPalliniC CPallini

            While p[-1] is valid (why didn't you try to compile it?), you are using it the wrong way: a char pointer (char *) is expected by the format specifier "%s" while you are passing a char ( p[-1], is like *(p-1), that is a char). You should have written either:

            printf("%s\n", &p[-1]);

            producing output

            est

            or

            printf("%c\n",p[-1]);

            , producing output

            e

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

            S Offline
            S Offline
            sakthimuhuu
            wrote on last edited by
            #5

            But, when i executed the program in Visual studio i got run time error Thanks, Sakthi

            CPalliniC 1 Reply Last reply
            0
            • S sakthimuhuu

              But, when i executed the program in Visual studio i got run time error Thanks, Sakthi

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

              What program? What error? (Please provide both).

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • S sakthimuhuu

                I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi

                S Offline
                S Offline
                Satya Chamakuri
                wrote on last edited by
                #7

                Hi, int main() { char *p = "Test"; p++; p++; printf("%s", p[-1]); getchar(); return 0; } Suppose think the starting address of p will be 0x20000, You are incrementing p two times, so before entering to the highlighted line in above code value of p is 0x20002 and the content in that location as starting address will be st. Then p[-1] means *(p-1), means value at 0x20001 location. means 'e'. In printf, %s is a format specifier wich will try to display the string at the memory location provided in variable list. Here the address provided in variable list is value of *(p-1), i.e, 'e' (internally this is 0x65). So %s dereferences the value at 0x65, so some garbage value it will print finally. otherwise will terminate the program as memory is un referenced. Thanks

                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