Thread: chars and pointers

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    chars and pointers

    My code is generaing a compile error on the append line. it doesn't like ch[1]. i tried "ch" which compiled fine but gives the pointer or something else very very odd.

    The function sends in a line of numbers and characters and returns just the numbers. Useful for stripping numeric fields of common delimiters (-,.#)
    Code:
    void ReduceVal(std::string& strval)
    {
    	char ch[1];
    	std::string::iterator p = strval.begin();
    	std::string newval = "";
    	
    	while(p != strval.end()) 
    	{
    		ch[1] = (char) (*p);
    		if (isdigit(ch[1]))  
    		{
    			newval.append(ch[1]);
    		}
    		p++;
    	}
    	strval = newval;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well ch[1] is only one char, and it's subscript is therefore ch[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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    SO I defint it as char ch[1] but I reference it as ch[0]?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    still doesn't like it.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    quick fix'd
    Code:
    		if (isdigit(ch[1]))  
    		{
    			tempval = ch[1];
    			newval.append(tempval);
    		}

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why are you even using an array of 1 characters when a simple ch will suffice?

    I doubt your fix, given your prior declarations - it's still an out of bound access even if it seems to be working.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> SO I defint it as char ch[1] but I reference it as ch[0]?

    Arrays use zero-based indexes in C++. When you define the array, the number in the brackets is the size. If you put 10, then your array will have room for ten characters. If you put 1, your array will have room for 1 character. When you use the array, the number in the brackets is the index. Since indexes start at zero, the valid indexes are 0 through (size-1). So for an array of size 10, indexes 0-9 are valid. For an array of size 1, only index 0 is valid.

    As for your code, if (isdigit(*p)) should work fine.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    THanks for the help. I've reviewed it and was able to shorten it.
    Code:
    void ReduceVal(std::string& strval)
    {
    	std::string::iterator p = strval.begin();
    	std::string newval = "";
    	std::string tempval = "";
    	
    	while(p != strval.end()) 
    	{
    		if (isdigit(*p) ) 
    		{
    			tempval = *p;
    			newval.append(tempval);
    		}
    		p++;
    	}
    	strval = newval;
    
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You don't need to set new string values to "", the default constructor will automatically make them empty:
    Code:
    std::string newval;
    Also, tempval is unnecessary. To append a single character to a string you can use any of the following:
    Code:
    newval += *p;
    newval.push_back(*p);
    newval.append(1, *p);

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    thanks, daved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with pointers to a list of chars
    By Brimak86 in forum C Programming
    Replies: 11
    Last Post: 01-14-2008, 09:15 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Replies: 2
    Last Post: 01-10-2005, 11:14 PM
  4. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:53 PM
  5. Array of pointers (chars)
    By Vber in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 04:29 AM