This question already has answers here:
Modifying String Literal [duplicate]
(4 answers)
Closed 8 years ago.
I wanna split a string by '/' and change char '/' to '/0' in the string, so I wrote a function like this:
void parse_query(char* str){
char* p = str;
char** r = (char**)malloc(sizeof(char*)*5);
int i = 0;
r[i++] = p;
while(p=strchr(p,'/')){
*p = '/0';
p++;
r[i++] = p;
}
}
When I ran the program like below:
char* s = "a/b";
parse_query(s);
the segmentation fault occurred at this line:
*p = '/0';
Can anyone give me a suggestion?
When I ran the program like below:
char* s = "a/b";
So you are modifying the string literal "a/b", which is undefined behaviour. If you want to modify it, then use an array like this:
char s[] = "a/b";
parse_query(s);
In addition, you should do (as noted by AntonH):
*p = '\0';
or
*p = 0;
to terminate the string. '/0' is the different from '\0'.
Replace:
*p = '/0';
which is not actually one character, but two, with:
*p = '\0';
Which is replacing the value pointed to by p with a value of zero. Which is what I believe you want.
Apart from the fact that it should be '\0', I think the segmentation fault stems from the fact that the string "a/b" is a literal, and these are generally stored in read-only memory. This means you probably can't write to that memory, and if you try, you get a segmentation fault.
Make a copy of the literal string into a writeable buffer and try it on that. I bet it works then.
Related
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 1 year ago.
Good day,
I have a function ft_strupcase which takes in a char*, upper-cases it, and returns the parameter. The issue arose during the testing, namely using the function in a main. The following program results in a segmentation fault:
int main()
{
char *hey = "hEy";
printf("%s\n", ft_strupcase(hey));
}
whereas this variation doesn't:
int main()
{
char hey[] = "hEy";
printf("%s\n", ft_strupcase(hey));
}
Isn't *str and str[] the same? Doesn't str[i] = *(str + i)? Why do I encounter a segfault then?
int main()
{
char *hey = "hEy";
printf("%s\n", ft_strupcase(hey));
}
In this code, hey points to a string literal, which is a constant. Then ft_strupcase modifies the thing you pass it a pointer to. So this code attempts to modify a constant.
You can't modify a constant. That's what it means for something to be constant.
int main()
{
char hey[] = "hEy";
printf("%s\n", ft_strupcase(hey));
}
Here, hey is an array of characters, initialized from a constant. The array is modifiable since the array entries are not constants.
If you have int i = 3;, you can modify i, but you can't modify the 3. The first code tries to modify the thing on the right side of the = by passing a function that modifies the thing pointed to a pointer to it. The second code modifies the thing on the left side of the =, which is perfectly legal.
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.
This question already has answers here:
Access violation when using strcpy?
(8 answers)
Closed 9 years ago.
#include <stdio.h>
char *strcpy_r(char* s, char* t);
int main()
{
char *s = {"Bob"};
char *t = {"Billy"};
char *ptr;
ptr = strcpy_r(s, t);
printf("%s\n", ptr);
return 0;
}
char* strcpy_r(char* s, char* t)
{
if((*s = *t) != '\0')
strcpy_r(s + 1, t + 1);
return s;
}
I'm just doing this for practice, but when I compiled it. I got a seg fault from main. Could someone tell me what might've caused this seg fault?
Congratulations, you have invoked undefined behavior twice within one line.
First, you can't modify the contents of a string literal. So strcpy()ing onto "foo" is wrong.
Two, even if you could: you're copying a string to a buffer that is shorter than the string. This is UB again.
You are trying to modify a constant string. This is wrong! Chances of segfault live when you modify a constant string.
Instead do this:
char s[10] = "Bob";
char t[10] = "Billy";
char *ptr;
You can't overwrite the memory that's used to hold a quoted string. That'll segfault instantly.
String literals are constant, i.e. they cant change. You're also trying to copy a longer string into a shorter string, which will write beyond the bounds of the destination string.
Both of these problems leads to undefined behavior which can cause a crash.
To solve the first problem, you have to use an array for the destination string. To solve the other problem, you have to make sure the destination array is at least as large as the source string (including its terminating '\0').
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
I want to replace a word in a string. Here is the code
char text[] = "This is a list of lists";
char *find = "list";
char* pos = NULL;
pos = strstr(text,find);
strncpy(pos,"test",4)
This works fine but
char *text = "This is a list of lists";
char *find = "list";
char* pos = NULL;
pos = strstr(text,find);
strncpy(pos,"test",4)
This gives a segmentation fault.
In the first example "text" is an array and the data is just copied at that location. In the 2nd one "text" is a pointer. What is issue?
The difference between
char text[] = "This is a list of lists"; // 1
and
char *text = "This is a list of lists"; // 2
is that, in (1), text is a non-constant array of characters; where as in (2), text points to a string literal, and string literals are are constant. You can't modify constant objects, which you're trying in (2). What you're doing in (2) in actually undefined behaviour.
The problem is the string in the second example is a string literal, which must remain constant. When you try to write on that string you are writing to read-only memory, which (depending on the operating system) is not allowed.