Thread: Segmentation Error

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    Segmentation Error

    So I have this code that's supposed to reverse a string and upon printing of the returned result I get a segmentation error.

    Now I put in several printf statements to test and I've found that the error is caused by the strcpy function.

    I'm confused as to why this is happening, could someone tell me how I can fix it?

    Code:
    char *reverse(char *input) {
    	int i; 
    	int j = 0;
    	char *temp = calloc(100, sizeof(char));
    	for(i=strlen(input)-1; i>=0;i--) {
    		temp[j] = input[i];
    		j++;
    	    }
    	temp[j] = '\0';
    	strcpy(input, temp);
    	free(temp);
    	return input;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How is the space for input originally being allocated? Is it an array? malloc()'d? A string literal???
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    calloc'd

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *reverse(char *input) {
    	int i; 
    	int j = 0;
    	char *temp = calloc(100, sizeof(char));
    	for(i=strlen(input)-1; i>=0;i--) {
    		temp[j] = input[i];
    		j++;
    	    }
    	temp[j] = '\0';
    	strcpy(input, temp);
    	free(temp);
    	return input;
    }
    
    
    int main(void) {
      char *rstring = calloc(100, sizeof(char));
    
    
    
    strcpy (rstring, reverse("hello."));
    
    
    /*	printf( "%s\n", rstring ) ; */
    	return 0;
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    "hello" is a string literal, which can only be read but you're trying to modify it in the strcpy(input,temp); statement. The solution would be to malloc input before strcpy in the reverse function.
    Last edited by BEN10; 12-05-2009 at 09:58 PM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Okay, that helped, thanks.
    Last edited by mrdibby; 12-06-2009 at 05:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM