Segmentation fault (core dumped)

This is a discussion on Segmentation fault (core dumped) within the C Programming forums, part of the General Programming Boards category; I do not understand where the error is. The function should reverse a string. Code: #include <string.h> char* reverse (char ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    11

    Segmentation fault (core dumped)

    I do not understand where the error is.
    The function should reverse a string.
    Code:
    #include <string.h>
    
    char* reverse (char string [])
    {
        int cntr = 0, len = (strlen (string) - 1);
        char ch;
        while (cntr < len)
        {
            ch = string[cntr];
            string[cntr] = string[len];
            string[len] = ch;
            ++cntr;
            --len;
        }
        return string;
    }
    
    int main(){reverse("hello.");}
    Last edited by benjaminp; 10-10-2012 at 01:06 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Works fine for me. What input are you passing to the function? How is that string declared? Please provide more complete code in the future, along with what input causes it to crash.

    I would bet you're doing something like:
    Code:
    char *p = "Some string to reverse";
    reverse(p);
    The problem with that is that p is just a pointer. It points to a string constant/literal, which is typically placed in read-only memory (it's constant, you shouldn't be changing it). Try declaring it as an array and initialize it:
    Code:
    char arr[] = "Some string to reverse";
    reverse(arr);
    That creates an array on the stack (writable memory) and initializes it with the value in the string literal, so you can reverse it in place.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    String literals are non-modifiable arrays of char. Try copying the string literal to a "standard" (modifiable) array and pass that to the function

    Code:
    int main(){char a[]="hello.";reverse(a);}
    Note: your program has no output.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    11
    the full code is:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char* reverse (char string []);
    void print_string (char* string);
    
    
    int main (void)
    {
    	print_string (reverse ("Hello."));
    	print_string ("\n");
    	return 0;
    }
    
    
    char* reverse (char string [])
    {
    	int cntr = 0, len = (strlen (string) - 1);
    	char ch;
    	while (cntr < len)
    	{
    		ch = string[cntr];
    		string[cntr] = string[len];
    		string[len] = ch;
    		printf ("string[cntr] == %c\nstring[len] == %c\ncntr == %i\nlen == %i\n\n", string[cntr], string[len], cntr, len);
    		++cntr;
    		--len;
    	}
    	return string;
    }
    
    
    void print_string (char* string)
    {
    	char c;
    	int counter = 0;
    	while ((c = putc (*(string + counter), stdout)) != '\0')
    		++counter;
    	return;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Yep, exactly what we said. You can't use a string literal. You must use a modifiable array of char. Don't pass "Hello." directly into your reverse function, copy it into a regular char array (make sure it's big enough for the whole string plus 1 byte for the null terminator).

    Also, for print_string, you could just use printf("%s", string).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Right, well you have ben told that you can't pass a string literal to that function because you cant modify string literals. You have been shown what you may do instead.

    Perhaps you could post code showing that you've done as has been explained.
    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"

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    11
    Now it is working properly:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char* reverse (char string []);
    void print_string (char* string);
    
    
    int main (void)
    {
    	char str [] = "Hello.";
    	print_string (reverse (str));
    	print_string ("\n");
    	return 0;
    }
    
    
    char* reverse (char string [])
    {
    	int cntr = 0, len = (strlen (string) - 1);
    	char ch;
    	while (cntr < len)
    	{
    		ch = string[cntr];
    		string[cntr] = string[len];
    		string[len] = ch;
    		++cntr;
    		--len;
    	}
    	return string;
    }
    
    
    void print_string (char* string)
    {
    	char c;
    	int counter = 0;
    	while ((c = putc (*(string + counter), stdout)) != '\0')
    		++counter;
    	return;
    }
    Last edited by benjaminp; 10-10-2012 at 01:42 PM.

  8. #8
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by benjaminp View Post
    why?
    i can write so: char ar [] = "hello";
    why can't i do this with functions?
    Because the first option copies the data in the string literal to the array, then uses the array as function arguments;
    and the second option uses the string literal itself (which is non-modifiable) as function arguments.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    11
    thanks. problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By Kevin Jerome in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2012, 01:58 AM
  2. Segmentation fault (core dumped)
    By mrbotts in forum C Programming
    Replies: 2
    Last Post: 01-10-2012, 11:06 AM
  3. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:37 AM
  4. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 08:50 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

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