c - Segmentation fault occurring when modifying a string using pointers? -


context

I am learning C, and I want to reverse the string using a space I'm trying to give the hints. (I know that you can use an array, it is more about learning about pointers.)

problem

While trying to run code below I'm getting split error. It looks like * end = * start; Not like the line Why is that so?

Especially when my code is almost identical

  #include & lt; Stdio.h & gt; #include & lt; String.h & gt; Zero my_strrev (four * start) {char temp; Four * end; End = begin + strlen (start) - 1; While (end> started) {temp = * end; * End = start * * Start = temporary; End--; ++ start; }} Main () {char * string = "foobar"; My_strrev (string); Printf ("% s", string); }   

One problem is that with the parameter passing the function: < / P>

  char * string = "foobar";  

This is a fixed string allocated in the read-only part.

  * end = * starts when you try to overwrite it;  

You will receive segfault.

Try

  Four String [] = "foobar";  

And you should notice the difference.

The main point is that in the first case, the string is present in the read-only segment and is simply an indicator on it in the second case while the array of characters with appropriate size has been reserved on the pile and fixed The string (which is always present) is copied to it. After that you are free to modify the contents of the array.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -