STRCPY seg fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode2
    New Member
    • Apr 2009
    • 32

    STRCPY seg fault

    I know this is a common forum post but I don't see how they fit with my code/problem:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void cds( char *cdsprefix )
    {
    
        // default
        char *prefix = "somereally_long_string_09";
    
        if( cdsprefix )
        {
    
    	printf("we passed in a prefix...\n\n");
    
    	printf("strlen of prefix : %d\n", strlen(prefix)); //25
    	printf("strlen of input : %d\n", strlen(cdsprefix)); //3
    	
    
    	strcpy( prefix, cdsprefix ); //SEG FAULT HERE!!!!!!!!!!
    
    	printf("i guess that strcpy passed...\n\n");
        }
    
        printf("now prefix string is : %s\n\n\n", prefix);    
    
    }
    
    
    
    int main(void)
    {
    	
    	char *abc = "abc";
    	char *rest = NULL;
    	rest = (char *)(malloc(strlen(abc) * sizeof(char) + 1 ));
    
    	strcpy( rest, abc );
    
    	printf("rest is now : %s \n\n\n", rest);
    
    
    	/////////////other test ///////////////
    
    	// we pass in rest
    	cds(rest);
    
    
    	return 7;
    }


    please help, i have performed many experiements and can't pin point the issue. thank you
  • dissectcode2
    New Member
    • Apr 2009
    • 32

    #2
    I FOUND THE ANSWER!!!



    relief!!

    Comment

    Working...