Pointer Incrementing

This is a discussion on Pointer Incrementing within the C Programming forums, part of the General Programming Boards category; This is from the ENVIRON.C program in Petzold's win32 book. Code: void FillListBox (HWND hwndList) { int iLength ; TCHAR ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Pointer Incrementing

    This is from the ENVIRON.C program in Petzold's win32 book.

    Code:
    void FillListBox (HWND hwndList) 
    {
         int     iLength ;
         TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName ;
    
         pVarBlock = GetEnvironmentStrings () ;  // Get pointer to environment block
    
         while (*pVarBlock)
         {
              if (*pVarBlock != '=')   // Skip variable names beginning with `=`
              {
                   pVarBeg = pVarBlock ;              // Beginning of variable name
                   while (*pVarBlock++ != '=') ;      // Scan until `=`
                   pVarEnd = pVarBlock - 1 ;          // Points to `=` sign
                   iLength = pVarEnd - pVarBeg ;      // Length of variable name
    
                        // Allocate memory for the variable name and terminating
                        // zero. Copy the variable name and append a zero.
    
                   pVarName = (TCHAR *) calloc (iLength + 1, sizeof (TCHAR)) ;
                   CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
                   pVarName[iLength] = '\0' ;
    
                        // Put the variable name in the list box and free memory.
                   SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
                   free (pVarName) ;
              }
              while (*pVarBlock++ != '\0') ;     // Scan until terminating zero
         }
         FreeEnvironmentStrings (pVarBlock) ;
    }

    When does pVarBlock become =0 and therefore false in the first while loop? It seems to me that this code will cause pVarBlock to keep increasing (>0) until it points to the end of the computers memory. The program shouldn't ever exit the while loop.

    Since the program works I am obviously missing something and it's probably pretty basic. Can someone explain?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    GetEnvironmentStrings Function (Windows)
    Look carefully at what is stored at the end of the strings.

    > while (*pVarBlock++ != '\0') ;
    This leaves pVarBlock pointing to the char after the \0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    while (*pVarBlock++ != '\0') ;     // Scan until terminating zero
    Quote Originally Posted by Salem View Post
    This leaves pVarBlock pointing to the char after the \0
    Probably this memory was calloc'd so will be all zeros after the last string.
    Last edited by MK27; 02-25-2010 at 11:19 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Quote Originally Posted by Salem View Post
    GetEnvironmentStrings Function (Windows)
    Look carefully at what is stored at the end of the strings.

    > while (*pVarBlock++ != '\0') ;
    This leaves pVarBlock pointing to the char after the \0
    Uhmm, yea. But that just positions it to the start of the next environment string so that it can process the next environment variable and get it's name. Even if it did increment pVarBlock until the end of all of the environment variable names and values, wouldn't pVarBlock still be nonzero and therefore true?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zoowho View Post
    Even if it did increment pVarBlock until the end of all of the environment variable names and values, wouldn't pVarBlock still be nonzero and therefore true?
    The value of pVarBlock is not the same as the value of *pVarBlock.

    Try this:
    Code:
    	char str[]="adsfa\0x", *ptr=str;
    	while (*ptr++);
    
    	printf("%c\n",*ptr);
    while (*ptr++) is identical to while(*ptr++ != 0)
    Last edited by MK27; 02-25-2010 at 11:46 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21