Reverse C string - simple bug [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to modify a string of char in C?
#include <stdio.h>
void reverseStr(char *str);
main()
{
reverseStr("abcdef");
}
void reverseStr(char *str) {
char *tmp = str;
char curr;
while (*tmp != '\0') {
tmp++;
}
tmp--;
while (tmp > str) {
curr = *str;
*str = *tmp;
*tmp = curr;
str++;
tmp--;
}
}
When I run it, I get:
/usr/bin/runit/srun_c: line 12: 2809 Segmentation fault /tmp/run_c_executable
What on earth is going on? I'm practicing for an interview, I'm rusty in my C and wanted to practice something easy but can't for the life of me figure this out.
I've noticed the seg fault disappears when I comment out the *str = *tmp; line, and I don't see why that should cause a seg fault.
Help appreciated.

You can't modify constant strings. Use a char array instead:
char str[] = "abcdef";
reverseStr(str);

You can't modify string literals — they're stored in readonly memory.
Use:
char str[] = "abcdef";
reverseStr(str);

Your reversal function looks fine. But it is the way you are calling the function that is causing this crash. You are passing a string literal which are read-only to the function. And modifying a string literal is a undefined behavior, manifesting as crash in your case.
Change
reverseStr("abcdef");
to
char str[] = "abcdef";
reverseStr(str);
where you pass a character array to the function.

Related

Why does strtok segfault when given a *string but not when given a string[]? [duplicate]

This question already has answers here:
strtok wont accept: char *str
(3 answers)
Closed 7 years ago.
Consider the following program
#include <stdio.h>
#include <string.h>
int main() {
char *str = "This is a test.";
char *token;
token = strtok(str," ");
}
It will segfault. But if I change *str to, say, str[80], it doesn't. Why is this?
Thanks
The trouble is that the type is wrong.
char *str = "This is a test.";
It may say char* but the thing on the right is actually char const* (C is very lax in allowing type punning (is that correct word)). Any attempt to modify a const is undefined behavior.
The function strtok() actually modifies the underlying string (by inserting '\0') so this is undefined behavior.
Fix by doing.
char str[] = "This is a test.";

EXC_BAD_ACCESS in code from Cracking the coding interview

I tried to run a reverse string function by using the code from Cracking the coding interview. I don't know if the code is wrong or I should use another IDE (I used Xcode 5.2 for this one). I'm new to C programming so please explain. Thanks.
#include <stdio.h>
void reverse(char *str);
int main(int argc, const char * argv[])
{
char *str = "Hello, World!";
reverse(str);
printf("%s", str);
return 0;
}
void reverse(char *str){
char *end = str;
char tmp;
if (str) {
while (*end) {
++end;
}
--end;
while (str < end) {
tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
The problem here is that you try to modify a string literal (str points to that) and string literals are constant (i.e. read-only, non modifiable).
Trying to modify something constant is undefined behavior. Undefined behavior may sometimes seem to work, but most of the time it will cause a crash at runtime.
A good advice to find possible cases of undefined behaviors (or other things that might be bad) is to enable more warnings. Then you should get a warning about assigning a constant array (all string literals are constant arrays) to a non-constant pointer.
char *str = "Hello, World!";
make it
char str[] = "Hello World!";

Segmentation Error in C Code [duplicate]

This question already has answers here:
Why can't I use this code to overwrite a string?
(4 answers)
Closed 9 years ago.
I am totally new to C and I am about to write a function that reverses a string. My code looks like this:
char *str = "abcdef";
char *ptr;
for(ptr = str; *ptr ; ptr++);
for(; str < --ptr; str++)
{
char c = *str;
*str = *ptr;
*ptr = c;
}
I get a segmentation fault error. And I don't see the mistake (maybe it is too obvious). Any hints?
Cheers
Change
char *str = "abcdef";
to
char str[] = "abcdef";
The first str points to a string literal and string literals are not modifiable in C,
Your string is stored in ROM, so you can't write to it. Depends on your platform though.

Segmentation fault when reversing string in C [duplicate]

This question already has answers here:
Reverse a string in C solution segfaulting
(2 answers)
Closed 10 years ago.
I have the following code which its main purpose is reverse the characters of a string. So, for example, the string I love cats would be converted to stac evol I.
#include <string.h>
#include <stddef.h>
#include <stdio.h>
void reverseString(char *str)
{
int size = strlen(str);
char *end = str + size - 1;
char tmp;
while (end > str) {
tmp = *str;
*str = *end;
*end = tmp;
end--;
str++;
}
}
int main()
{
char *str = "Y U SEGMENTATION FAULT?";
reverseString(str);
}
When I run this, I get a segmentation fault, and I fail to see why. Also, another question I have is the time complexity (Big O) of this function. I believe it should be O(n/2), since I am not going through all the array but just the half of it. Am I right?
You are trying to modify a character literal, a string in read only data segment. Make a copy/duplicate of it on the heap with strdup, for example:
char *str = strdup("It's OK now");
Or make it a local array (place the string on the stack):
char[] str = "It's OK now";

Increment character value of a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
Here is a small function, was testing something so wrote it. Here i tried to increment a character value of the string literal when i tried doing so i got a segmentation fault. Can you please tell what i am doing wrong here
#include <stdio.h>
int input_string(char *str)
{
printf("%s\n", str);
printf("%c\n", *str);
printf("%c\n", (*str)++); // I get a segmentation fault here, cant i increment the value like this ?
}
void main()
{
char *str = "andrew";
input_string(str);
}
What this char *str = "andrew"; does is create a pointer to a string that MAY be located on .text (where the executable code resides) and trying to modify it is undefined behavior.
Change it for this:
char str[] = "andrew";
It will make a copy of the string in a stack allocated buffer that you can safely modify.
This:
char *str = "andrew";
means what str points to a constant string literal. You will get undefined behavior if you try to change it.
If you want to perform string manipulation, define a character array.

Resources