Thread: pointer comparison failing

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    pointer comparison failing

    I am trying to write my own implementation of GetSystemDirectory, and my pointer comparison seems to be failing.

    Code:
    UINT xGetSystemDirectory(char *pszDest, UINT iLen)
    {
    	char szBuf[MAX_PATH], *p, *q;
    	unsigned i;
    
    	GetModuleFileName(GetModuleHandle("kernel32"), szBuf, sizeof szBuf - 1);
    
    	q = strrchr(szBuf, '\\');
    
    	for(i = 0, p = pszDest; i < iLen && p != q; *p++ = szBuf[i], i++);
    
    	*p = '\0';
    	
    	return i;
    }
    this function produces: C:\windows\system32\kernel32.dll in pszDest. comparing p to q seems to be failing. the loop should be stopping at the last backslash, the result should be: C:\windows\system32

    any help is greatly appreciated. thanks in advance.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    p points to pszDest, which points to a string. code is cramped, but that's my style.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does p point into the same character array as q?

    (Edit: sorry, deleted previous post, doubting if p is pointing to valid memory.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    I am trying to write my own implementation of GetSystemDirectory,
    *ouch*

    However, this hint could be helpful:

    Code:
    q = strrchr(szBuf, '\\');
    
    *q = '\0';  /* cut off "\kernel32.dll" */
    Then continue using strcpy and strlen instead of that for() loop.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thanks guys.

    anon: no, p didn't point to the same array as q, that was the problem.

    rpet: I had tried that and actually considered sticking with it, however that would render the iLen parameter useless and I really didn't want to do it with strncpy.

    here it is fixed:

    Code:
    UINT xGetSystemDirectory(char *pszDest, UINT iLen)
    {
    	char szBuf[MAX_PATH], *p, *q, *r;
    	unsigned i;
    
    	GetModuleFileName(GetModuleHandle("kernel32"), szBuf, sizeof szBuf - 1);
    
    	q = strrchr(szBuf, '\\');
    
    	for(i = 0, p = pszDest, r = szBuf; i < iLen && r != q; *p++ = *r++, i++);
    
    	*p = '\0';
    	
    	return i;
    }

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM