Results 1 to 4 of 4

Thread: Test for a variable or not

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    Test for a variable or not

    Lets say I have code like this:

    Code:
    inline int Len(char *p_chString){int length = 0;while( *p_chString++ ) ++length; return length;}
    
    void RTrim(char *p_chString)
    {
    /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
    	char *buf;
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
    
    	if(*p_chString == 0 || *p_chString == 0x00)	//If String Is Not Blank Or Errored
    		return;
    
    	buf = p_chString;
    	buf += Len(p_chString) - 1;
    
    	while (*buf == ' ' && Len(p_chString) )
    		*buf-- = '\0';
    }
    Which trims spaces from the right side of a string. Now if I use it like so it works great:

    Code:
    	char cTemp[10];
    	strncpy(cTemp," t    ",10);
    	RTrim(cTemp);
    Like it should. But lets say a stupid user comes along and tries this:

    Code:
    RTrim("SPACE    ");
    Where you are not passing a variable. How can I test for that?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    You dont. Document how the function is supposed to be called. If you are really worried, make a second parameter to be a destination buffer, that should be initialized before the function is called.

    Z.

  3. #3

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Thats what I plan to do, but I figured I would check. I thought maybe you could check the param to make it had a vaild memory allocation to it or some thing.

    [EDIT]
    YEAH POST 800!!
    Last edited by Technocrat; Jun 5th, 2003 at 10:44 AM.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    On a proper compiler it simply wouldn't be possible because "SPACE " would be of type const char * and you function expects char *.

    But VC++ isn't a proper compiler...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width