This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 2 months ago.
I'm trying the squeeze program from K&R. However, I keep getting a bus error. The code below directly triggers the problem. Shouldn't this be portable?
int main() {
char* str = "foo";
for (int i = 0, j = 0; str[i]; ++i)
str[j] = '.';
}
The function from the book:
void squeeze(char s[], int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] = s[i];
s[j] = '\0';
}
str points to a string constant. Such constants are read only, so attempting to modify them triggers undefined behavior which in your particular case manifests in the program crashing.
Instead, declare str as an array, which can be modified:
char str[] = "foo";
Related
This question already has answers here:
Why does this C program throw a segmentation fault at runtime?
(3 answers)
using pointer to char vs char array [duplicate]
(1 answer)
Closed 7 months ago.
I was trying to reverse a string stored in a pointer but I keep receiving the error
EXC_BAD_ACCESS (code=2, address=0x100003f7c)
when code enters the for loop.
It works if I change char *s to char s[ ] .
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s = "hello";
char c;
int i,j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--)
{
c = s[i], s[i] = s[j], s[j] = c;
}
printf("%s",s);
}
Code editor - Visual Studio Code
OS - macOS
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed last year.
Trying to change char by index:
char *p = "test";
for (int i = 0; i < 4; ++i) {
p[i] = 50;
}
printf("%s\n", p);//test
What I'm doing wrong?
you cannot change a literal,
char*p="test";
doesnt copy the literal. You should do
char p[] = "test";
or
#include <string.h>
....
char *p = strdup("test");
if you want to deal with a pointer rather than a char array.
The line
char *p = "test";
will make the pointer p point to the string literal "test".
String literals are read-only. Attempting to modify them will invoke undefined behavior.
If you want "test" to be writable, then you must allocate (writable) memory for it, for example by declaring an array:
char arr[] = "test";
That line is equivalent to
char arr[5] = "test";
which creates an array of 5 characters and initializes the first 4 characters to "test" and initializes the 5th character to a null terminating character.
Now that you have allocated memory for the string, you can write to it at will, for example you could write:
for (int i = 0; i < 4; ++i) {
arr[i] = 50;
}
Or you could make a pointer p point to the array, and use the pointer for accessing the string, as you did in the code in the question:
char *p = arr;
for (int i = 0; i < 4; ++i) {
p[i] = 50;
}
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 5 years ago.
#include<stdio.h>
#include<string.h>
int main(){
char *a[]={"this","is","a","string"};
char temp[100];
for(int i=0;i<3;i++){
for(int j=0;j<3-i;j++){
if(strcmp(a[j],a[j+1])>0){
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}
for(int i=0;i<4;i++){
printf("%s\n",a[i]);
}
}
The following code is for bubble sorting the strings.It gives segmentation fault.What is wrong in it?
String literals are read-only in C. Your strcpy(a[j],a[j+1]) and strcpy(a[j+1], temp) calls are illegal.
There is no need to use strcpy. The only thing that you have to do is to swap a[j] and a[j+1] pointers.
Make sure that the for loop have the correct bounds. The array has size 4, and valid elements are in the range [0..4)
Take a look at the following:
int main(){
char const *a[]={"this","is","a","string"}; //const added
char *temp = 0; // a simple pointer
for(int i=0;i<4;i++){ //bounds changed
for(int j=0;j<4-1;j++){
if(strcmp(a[j],a[j+1])>0){ // swap pointers
temp = a[j];
a[j] = a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<4;i++){
printf("%s\n",a[i]);
}
}
This question already has answers here:
Access violation error when reversing a string c++ [duplicate]
(1 answer)
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 9 years ago.
On line 18, I get a seg fault in the first iteration (i = 0).
#include <stdio.h>
int main(void) {
char* str = "mono";
int length = 0;
int i;
for (i = 0; ; i++) {
if (str[i] == '\0') {
break;
} else {
length++;
}
}
for (i = 0; i < length / 2; i++) {
char temp = str[length - i - 1];
str[length - i - 1] = str[i]; // line 18
str[i] = temp;
}
printf("%s", str);
return 0;
}
I wrote this algorithm to reverse a string.
You are modifying a string literal:
char* str = "mono";
and string literals are non-modifiable in C.
To fix your issue, use an an array initialized by the string literal:
char str[] = "mono";
Runtime Error:
char* str = "mono"; // str points to an address in the code-section, which is a Read-Only section
str[1] = 'x'; // Illegal memory access violation
Compilation Error:
const char* str = "mono"; // This is a correct declaration, which will prevent the runtime error above
str[1] = 'x'; // The compiler will not allow this
All Good:
char str[] = "mono"; // str points to an address in the stack or the data-section, which are both Read-Write sections
str[1] = 'x'; // Works OK
Notes:
In all cases, a string "mono" is placed in the code-section of the program.
In the last example, the contents of that string are copied into the str array.
In the last example, the str array is located in the stack if str is a non-static local variable, and in the data-section of the program otherwise.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Getting Segmentation Fault
// reverse a string
#include`<stdlib.h>`
#include`<stdio.h>`
#include`<string.h>`
#include`<math.h>`
int main()
{
char *string = "mohit",t;
int i=0,j;
printf(" %d %d",strlen(string), (strlen(string)/2)+1);
for(i=0,j=(strlen(string)) ; i<(strlen(string)/2)+1 ; i++,j--)
{
printf("\n%d",(int) string);
printf("\n%d",(int) string+5);
printf("\ni string = %c", *(string + i));
printf("\nj string = %c", *(string + j));
t=*(string+i);
*(string+i) = *(string + j);
*(string + j) = t;
}
printf("\n = %s", string);
return 0;
}
String literals are possibly located in a read-only area of memory; it is not allowed to assign a pointer to one to a pointer to non-const char, and thus your manipulations of string cause undefined behaviour. You either say
const char * string = "mohit";
and don't modify the string, or you create an automatic array of chars that you can modify:
char string[] = "mohit";
The latter is what you need in your case.
Also, as a point of style, writing char * s, t; in one line is possibly misleading; it is equivalent to, and should always be replaced by, char * s; char t;.