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. while (*p++);

while (*p++);

Scheduled Pinned Locked Moved C / C++ / MFC
21 Posts 9 Posters 1 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
    Chintoo723
    wrote on last edited by
    #1

    How do you interpret this: while (*p++);

    J C S R C 6 Replies Last reply
    0
    • C Chintoo723

      How do you interpret this: while (*p++);

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      LABEL:
      if(*p) // if *p does not equal zero
      {
      ++p;
      goto LABEL;
      }

      Think about it! INTP Every thing is relative...

      A 1 Reply Last reply
      0
      • C Chintoo723

        How do you interpret this: while (*p++);

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Looks like someone is trying to write hard to read code, that does nothing. I believe that * gets called before ++, which would mean that a pointer is dereferened, and incremented, then the new value is returned. This code will run until that value is zero, and it's basically the same as *p = 0; but it will take a little longer. Christian Graus - Microsoft MVP - C++

        A 1 Reply Last reply
        0
        • J John R Shaw

          LABEL:
          if(*p) // if *p does not equal zero
          {
          ++p;
          goto LABEL;
          }

          Think about it! INTP Every thing is relative...

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          I actually thought about it, and your interpretation doesnt seem to match what is working. This piece of code is from: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining\_a\_file\_name\_from\_a\_file\_handle.asp where p initially points to a: A:\\0C:\\0D:\\0\0 where \0 represents a single NULL character.

          J 1 Reply Last reply
          0
          • C Christian Graus

            Looks like someone is trying to write hard to read code, that does nothing. I believe that * gets called before ++, which would mean that a pointer is dereferened, and incremented, then the new value is returned. This code will run until that value is zero, and it's basically the same as *p = 0; but it will take a little longer. Christian Graus - Microsoft MVP - C++

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            that someone is none other than a micrsoft sample code. This piece of code is from: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining\_a\_file\_name\_from\_a\_file\_handle.asp where p initially points to a: A:\\0C:\\0D:\\0\0 where \0 represents a single NULL character. I dont understand what you maen by *p = 0, but this piece of code doesnt modify any data, only pointer advancement. and how that works is still mysterious to me.

            C 1 Reply Last reply
            0
            • A Anonymous

              I actually thought about it, and your interpretation doesnt seem to match what is working. This piece of code is from: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining\_a\_file\_name\_from\_a\_file\_handle.asp where p initially points to a: A:\\0C:\\0D:\\0\0 where \0 represents a single NULL character.

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              I am surprised I even looked at that code. What I showed you was a litteral interpretation of the code you presented. I could even present it in assembly if required (which I will not do). Let's break it down: 1) Copy the current value into a temporary variable. 2) Increment the current value. 3) Check if the temporary value has a value other than zero. If it does not then stop looping. That is how it works! INTP Every thing is relative...

              C 1 Reply Last reply
              0
              • C Chintoo723

                How do you interpret this: while (*p++);

                S Offline
                S Offline
                sunit5
                wrote on last edited by
                #7

                execution will take place from right to left first the address store in p gets incremented then the derefrence of the pointer(new address stored in p) will take place. Finally the condition will be checked by while loop.If it is non zero statements enclosed by while loop braces will be exceuted otherwise not.

                G 1 Reply Last reply
                0
                • S sunit5

                  execution will take place from right to left first the address store in p gets incremented then the derefrence of the pointer(new address stored in p) will take place. Finally the condition will be checked by while loop.If it is non zero statements enclosed by while loop braces will be exceuted otherwise not.

                  G Offline
                  G Offline
                  Ghasrfakhri
                  wrote on last edited by
                  #8

                  first Compare value of *P & then inc P , *p++ used no *(++p) Iman Ghasrfakhri

                  S 1 Reply Last reply
                  0
                  • C Chintoo723

                    How do you interpret this: while (*p++);

                    R Offline
                    R Offline
                    Roger Stoltz
                    wrote on last edited by
                    #9

                    The statement searches for the first byte after a zero terminated string. It's useful when you're parsing a multistring, e.g. when you have a CFileDialog and the user has selected several files. The response comes in a string where the files are separated with a single NULL char and the file list end with a double NULL char. The statement mentioned would in such case find the next file name, or point to the second NULL char at the end of the list. -- Roger


                    It's supposed to be hard, otherwise anybody could do it!

                    1 Reply Last reply
                    0
                    • G Ghasrfakhri

                      first Compare value of *P & then inc P , *p++ used no *(++p) Iman Ghasrfakhri

                      S Offline
                      S Offline
                      sunit5
                      wrote on last edited by
                      #10

                      execution will take place from right to left first the address store in p gets incremented then the derefrence of the pointer(new address stored in p) will take place. Finally the condition will be checked by while loop.If it is non zero statements enclosed by while loop braces will be exceuted otherwise not. There was a mistake in my previous post.I have rectified it //first the derefrence of the pointer will take place and then according to that value conditon is checked //check the condition in while if(*p) { p++; // statements inside while loop are executed } else //when while loop fails {p++;} //statements after while loop -- modified at 6:12 Thursday 27th October, 2005

                      1 Reply Last reply
                      0
                      • C Chintoo723

                        How do you interpret this: while (*p++);

                        C Offline
                        C Offline
                        Carsten Leue
                        wrote on last edited by
                        #11

                        If p initially points to a character string then after while(*p++); p points to the 0 character that terminates the string. So it is semantically the same as p = p + _tcslen(p);

                        C 1 Reply Last reply
                        0
                        • J John R Shaw

                          I am surprised I even looked at that code. What I showed you was a litteral interpretation of the code you presented. I could even present it in assembly if required (which I will not do). Let's break it down: 1) Copy the current value into a temporary variable. 2) Increment the current value. 3) Check if the temporary value has a value other than zero. If it does not then stop looping. That is how it works! INTP Every thing is relative...

                          C Offline
                          C Offline
                          Chintoo723
                          wrote on last edited by
                          #12

                          With your solution p will point to the first NULL from the beginning, which is not really correct. it will point to the char after the first null. try to trace it and see. but be careful with the string you use, or your program might crash. for eg, use "a\0bb\0ccc\0\0dddd" I can understand, the code is very deceptive. only a few get it right the first time, and I am not one of them either :) thanks!

                          J 1 Reply Last reply
                          0
                          • C Carsten Leue

                            If p initially points to a character string then after while(*p++); p points to the 0 character that terminates the string. So it is semantically the same as p = p + _tcslen(p);

                            C Offline
                            C Offline
                            Chintoo723
                            wrote on last edited by
                            #13

                            Carsten Leue wrote:

                            p = p + _tcslen(p);

                            Welcome to the gang! you too got it incorrect :) it, actually, is equivalent to : p= p + _tcslen(p) + 1 thanks!

                            C 1 Reply Last reply
                            0
                            • C Chintoo723

                              How do you interpret this: while (*p++);

                              T Offline
                              T Offline
                              TheGreatAndPowerfulOz
                              wrote on last edited by
                              #14

                              step 1. obtain value of object pointed to by "p" step 2. increment "p" step 3. if value evaluates to "true" then execute statement and continue with step 1 step 4. stop

                              T 1 Reply Last reply
                              0
                              • T TheGreatAndPowerfulOz

                                step 1. obtain value of object pointed to by "p" step 2. increment "p" step 3. if value evaluates to "true" then execute statement and continue with step 1 step 4. stop

                                T Offline
                                T Offline
                                TheGreatAndPowerfulOz
                                wrote on last edited by
                                #15

                                equivalent code:

                                while (true)
                                {
                                    bool done = *p == 0;
                                    ++p;
                                    if (done) break;
                                }
                                
                                C 1 Reply Last reply
                                0
                                • A Anonymous

                                  that someone is none other than a micrsoft sample code. This piece of code is from: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining\_a\_file\_name\_from\_a\_file\_handle.asp where p initially points to a: A:\\0C:\\0D:\\0\0 where \0 represents a single NULL character. I dont understand what you maen by *p = 0, but this piece of code doesnt modify any data, only pointer advancement. and how that works is still mysterious to me.

                                  C Offline
                                  C Offline
                                  Christian Graus
                                  wrote on last edited by
                                  #16

                                  It actually does the ++ first, which means that it increments the pointer until it points to NULL. Christian Graus - Microsoft MVP - C++

                                  C 1 Reply Last reply
                                  0
                                  • C Christian Graus

                                    It actually does the ++ first, which means that it increments the pointer until it points to NULL. Christian Graus - Microsoft MVP - C++

                                    C Offline
                                    C Offline
                                    Chintoo723
                                    wrote on last edited by
                                    #17

                                    Christian Graus wrote:

                                    It actually does the ++ first, which means that it increments the pointer until it points to NULL.

                                    Welcome to the gang! you are not alone :) PS: check out other posts thanks!

                                    1 Reply Last reply
                                    0
                                    • T TheGreatAndPowerfulOz

                                      equivalent code:

                                      while (true)
                                      {
                                          bool done = *p == 0;
                                          ++p;
                                          if (done) break;
                                      }
                                      
                                      C Offline
                                      C Offline
                                      Chintoo723
                                      wrote on last edited by
                                      #18

                                      Roger, Sunit, Ahz- you guys are awesome! Does this example fit an obfuscated C contest? :) thanks!

                                      T 1 Reply Last reply
                                      0
                                      • C Chintoo723

                                        Roger, Sunit, Ahz- you guys are awesome! Does this example fit an obfuscated C contest? :) thanks!

                                        T Offline
                                        T Offline
                                        TheGreatAndPowerfulOz
                                        wrote on last edited by
                                        #19

                                        Lambu Jindu wrote:

                                        Does this example fit an obfuscated C contest?

                                        no

                                        1 Reply Last reply
                                        0
                                        • C Chintoo723

                                          Carsten Leue wrote:

                                          p = p + _tcslen(p);

                                          Welcome to the gang! you too got it incorrect :) it, actually, is equivalent to : p= p + _tcslen(p) + 1 thanks!

                                          C Offline
                                          C Offline
                                          Carsten Leue
                                          wrote on last edited by
                                          #20

                                          It's a bummer! You are correct. The loop ends if p points to the end of the string but p is still incremented. Carsten

                                          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