array and char confusion

This is a discussion on array and char confusion within the C++ Programming forums, part of the General Programming Boards category; Code: char buffer[256] = "abcde~fghij~klm" ===== char *full_parm(const char *bufferx, int parm_index ) { int parm1_bookmark = 0; int parm2_bookmark ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    array and char confusion

    Code:
    char buffer[256] = "abcde~fghij~klm"
    
    
    =====
    
    
    
    char *full_parm(const char *bufferx,  int parm_index )
    {
    
    	int parm1_bookmark = 0;
    	int parm2_bookmark = 0;
    	char temp[10];
    	int i = 0;
    	int j=0;
    
    	for(i=0; i<256; i++)  // bookmarks the 2 bookmarks
    	{
    	
    	
    		if (bufferx[i] == '~')
    		{
    			if (parm_index == 1)
    			{
    				for(j=0; j <= (i-1); j++)
    				{ *temp = bufferx[j];
    				temp++;
    				}
    				parm1_bookmark = (i+1);
    				return temp;
    			}
    			parm1_bookmark = ( i+1);
    		}
    		else if ( (bufferx[i] == '~') && (parm1_bookmark != 0)  )
    		{
    			if (parm_index == 2)
    			{
    				for(j=parm1_bookmark; j<= (i-1); j++)
    				{ *temp = bufferx[j];
    				temp++;
    				}return temp;
    			}
    			parm2_bookmark = (i+1);
    		}
    		else if ( (bufferx[i] == '~') && (parm2_bookmark != 0) )
    			if (parm_index == 3)
    			{
    				for(j=parm2_bookmark  ; j <=(i-1); j++)
    				{
    					*temp = bufferx[j];	
    					temp++;
    				}return temp;
    			}
    	}
    }

    basically what i am trying to do is to sections out the useful portion of the buffer and ignore the garbage.

    if i enter full_parm(buffer, 1)
    i hope function to return "abcde"
    if i enter full_parm(buffer, 2)
    i hope function to return "fghij"

    but i have been unsucessful so far
    i suspect it's me writing the arrrays wrongly
    any idea what's wrong with my code?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > temp++;
    Arrays cannot be incremented

    Use
    temp[index++] = something;

    Don't forget
    temp[index] = '\0';
    to make the string complete

    > return temp;
    You cant return the address of a local variable.
    The quick fix in this instance is to make your temp static, like so
    static char temp[10];

    > for(i=0; i<256; i++)
    Use strlen() of the input buffer.


    A better answer all over would be
    char *full_parm(const char *bufferx, int parm_index, char *temp, size_t temp_size )
    This is the fgets() approach - the caller provides the space and says how big that space is, and then life is much easier for the called function.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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