Thread: Left shift a string

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    Left shift a string

    Gudday all
    I wish to left shift a string by one char to remove a leading '\'. In concept it seems simple enough but I flounder on the strings handling still esp. initialisation, pointers etc:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <io.h>
    #include <stdlib.h>
    #include <cstring>
    
    int main(int argc, char *argv[])
    {
    	size_t size = 14;
    	char *dst[size]= {0};
    	char *src[size+1] = "\0000544D.TIF";
    
     while (size != 0) {
    	 dst[size] = src[size-1];
    	 size--;
    }
    }
    The error I get is
    C:/ScanImages/leftShiftACharacter.cpp: In function `int main(int, char**)':
    C:/ScanImages/leftShiftACharacter.cpp:13: error: variable-sized object `dst' may not be initialized
    C:/ScanImages/leftShiftACharacter.cpp:14: error: variable-sized object `src' may not be initialized
    It must be staring me in the face but I have lost it.

    I really find strings and pointers to be very challenging. Is there a good on-line reference that is not too rocket science?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That means just what it says -- either explicitly put 14 in the [] when you declare your arrays, or do not initialize them when you declare them.

    Also, the string "\0000544D.TIF" does not contain a '\' character -- it contains the characters '\0', '0', '0', '0', '5', '4', '4', 'D', '.', 'T', 'I', 'F', '\0' in that order.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Just add 1 to the pointer location.

  4. #4
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by tabstop View Post
    That means just what it says -- either explicitly put 14 in the [] when you declare your arrays, or do not initialize them when you declare them.

    Also, the string "\0000544D.TIF" does not contain a '\' character -- it contains the characters '\0', '0', '0', '0', '5', '4', '4', 'D', '.', 'T', 'I', 'F', '\0' in that order.
    Your comment about the string not containing '\' puzzles me. How then is a '\' expressed if it looks like a '\0'?
    How does C differentiate between a '\' and a '\0'?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The problem is that your trying to declare an array with the variable amount of size. Which is not allowed. Either you need to hard code size of the array you want or use a macro.

    You really require a char array; but not a array char pointers. so take off the leading '*' char from the array declarations.

    And your logic to eliminate the leading '\' and push all other characters to left won't work. Can you see why? size should really be initialised to 0.

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Tigers! View Post
    Your comment about the string not containing '\' puzzles me. How then is a '\' expressed if it looks like a '\0'?
    How does C differentiate between a '\' and a '\0'?
    Have you ever used \n before? Haven't you ever used \0 before? What about \t? Or \a? Or \"?

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >How does C differentiate between a '\' and a '\0'?
    '\0' is an escape sequence char. The parser would under stand all the escape sequnce and act accordingly. For the list of escape sequence char have a look the link.

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by ssharish2005 View Post
    The problem is that your trying to declare an array with the variable amount of size. Which is not allowed. Either you need to hard code size of the array you want or use a macro.

    You really require a char array; but not a array char pointers. so take off the leading '*' char from the array declarations.

    And your logic to eliminate the leading '\' and push all other characters to left won't work. Can you see why? size should really be initialised to 0.

    ~ssharish
    Now you have me worried as to what I am missing or have missed.

    I learnt/used C many years ago at uni. Trying to catch up again is proving to be a long process.

  9. #9
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by tabstop View Post
    Have you ever used \n before? Haven't you ever used \0 before? What about \t? Or \a? Or \"?
    Oh I have used those before.
    Strings have been a thorn in my side these last 3 months. Trying to remember when a \n is added and when it is not. Trying to remember when \0 should be there or not.

    It is very slowly coming back.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> char *src[size+1] = "\0000544D.TIF";

    That's an array of 14 char* - you probably want to drop the pointer.

    >> Now you have me worried as to what I am missing or have missed.

    If the string is a literal, then you'd need "\\" to represent a single "\", but that only applies to what the compiler "sees", not a string read from the user, a file, etc. At any rate, all that isn't necessary (I didn't compile this, but it should work fine):

    Code:
    char* remove_char( char* str, char fmt )
    {
    	char
    		* cpy, 
    		* pos;
    	for( pos = cpy = str; *pos; ++pos )
    		if( *pos != fmt )
    			*cpy++ = *pos;
    	*cpy = 0;
    	return str;
    }
    Or using indexes, instead:

    Code:
    char* remove_char( char* str, char fmt )
    {
    	int
    		cpy, 
    		pos;
    	for( pos = cpy = 0; str[ pos ]; ++pos )
    		if( str[ pos ] != fmt )
    			str[ cpy++ ] = str[ pos ];
    	str[ cpy ] = 0;
    	return str;
    }
    Example:

    Code:
    int main( void )
    {
    	char*
    		buf[ 1024 ] = "\0000544D.TIF";
    	puts( remove_char( buf, '\\' ) );
    	return 0;
    }
    Of course it strips all of the chars from the text, but a similar approach could be used to remove only the first one, if desired.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by carrotcake1029 View Post
    Just add 1 to the pointer location.
    Seconded

    Code:
    int main(int argc, char *argv[])
    {
        const char *src = "\\0000544D.TIF";
        printf("%s\n", src);  // WITH leading backslash
        printf("%s\n", src+1); // WITHOUT leading backslash
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM