chars and pointers

This is a discussion on chars and pointers within the C++ Programming forums, part of the General Programming Boards category; My code is generaing a compile error on the append line. it doesn't like ch[1]. i tried "ch" which compiled ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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,317
    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, 08: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, 10:14 PM
  4. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 08:53 PM
  5. Array of pointers (chars)
    By Vber in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 03:29 AM

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