Ummm, why isnt this working?

This is a discussion on Ummm, why isnt this working? within the C Programming forums, part of the General Programming Boards category; Code: int main(void){ int size, start; char *string = "Two words"; size = (int) strlen(string); start = 0; reverseString(string, start, ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Ummm, why isnt this working?

    Code:
    int main(void){
    	int size, start;
    	char *string = "Two words";
    
    	size = (int) strlen(string);
    	start = 0;
    
    	reverseString(string, start, size - 1);
    
    	return 0;
    }
    void reverseString(char *string, int start, int end){
    	char temp;
    
    	while(end > start){
    		temp = string[start];
    		string[start] = string[end];      /* <------ crashes here */
    		string[end] = temp;
    
    		start++; end--;
    	}
    	printf("reversed string: %s\n", string);
    }
    I'm trying to take a string: "Two words" and turn it into "sdrow owT".
    I guess there is something wrong with my function though.
    The program just hangs for a while after I execute it and then it crashes.
    Any suggestions? Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    The problem may be due to the fact that you are trying to change a string literal. Instead of:
    Code:
    char *string = "Two words";
    write:
    Code:
    char string[] = "Two words";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by laserlight View Post
    The problem may be due to the fact that you are trying to change a string literal. Instead of:
    Code:
    char *string = "Two words";
    write:
    Code:
    char string[] = "Two words";
    Yes, you are correct. Thank you very much.
    Do you care to explain why exactly that happens?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    Quote Originally Posted by gp364481
    Do you care to explain why exactly that happens?
    Undefined behaviour happens since you are not allowed to modify a string literal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because you cannot modify a string literal, just like you cannot modify one of these:
    Code:
    #define string "two words"
    const char string[] = "two words";
    Whereas you can modify a non-const array:
    Code:
    char string[] = "Two words";
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Okay, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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