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 4 years ago.
in this C program when i am using
printf("%c",*(name+5));
then program works fine but when i am using
*(name+5) = '#';
then program causes crash
#include <stdio.h>
void main(void)
{
char * name;
name ="Hello World !";
puts(name);
*(name+5) = '#'; // here is error
puts(name);
}
With...
char * name;
name ="Hello World !";
*(name+5) = '#';
you are manipulating the contents of a string literal, which is undefined behaviour, likely a crash.
Make an array out of it, which you may alter then:
char name[] ="Hello World !";
name[5] = '#';
or:
char buffer[] ="Hello World !";
char *name = buffer;
*(name+5) = '#';
Note that here the contents of string literal "Hello World!" are copied into an array which's content you are allowed to change.
Related
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 3 years ago.
#include <stdio.h>
#include <string.h>
void space_to_tab(char *string) {
char str [strlen(string)];
int size = 0;
while(string[size] != '\0'){
if(string[size] == ' ')
str[size] = '\t';
else
str[size] = string[size];
size++;
}
*string = *str;
}
int main() {
char *str = "aa b";
printf("%s\n", str);
space_to_tab(str);
printf("%s\n", str);
}
I just started with C programming and I want to switch the spaces in a string with tabs and I get the error "Segmentation fault (core dumped)".
I belive the error is in "*string = *str;" but I dont know how to change the pointer of one string to the other.
You should not modify literal strings (i.e. "aa b") as this results in undefined behaviour (hence the segmentation fault). Instead you should modify an array like such:
char str[] = "aa b";
See In C, can I initialize a string in a pointer declaration the same way I can initialize a string in a char array declaration?
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 4 years ago.
I am a beginner programmer. I wrote the following code to copy the one string into other, using pointers.
But I am not getting the output. The compiler says segmentation fault.
I have gone over and over the program, but to no avail. I am not able to locate the fault, and how to fix it.
It would be hard to believe but I have been stuck for almost 2 hours now.
Any help is greatly appreciated.
#include<stdio.h>
char *copy(char*, char*);
int main() {
char *str1 = "avanti";
char *str2 = "ujj";
printf("%s\n", str1);
char *result = copy(str1, str2);
printf("%s", result);
}
char *copy(char *str1, char *str2){
int i=0;
while (*(str2+i) != '\0') {
*(str1+i) = *(str2+i);
i++;
}
*(str1+i) = '\0';
return str1;
}
"avanti" is a string constant, not a place you can copy to. You might change it to char str1[] = "avanti"; This is an array of characters that is initialized with the value of the string constant "avanti"
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.
I am trying to reverse a string literal with the use of pointers, though my code received SEGSIGV signal on *head=*tail line
char* reverse(char* input, int n) {
char temp;
char* head= input;
char* tail= &input[n-1];
while(head<tail){
temp=*head;
*head=*tail;
*tail=temp;
head++;
tail--;
}
return input;
}
int main(void){
char* sentence= "All work and no play makes jack a dull boy";
reverse(sentence, strlen(sentence));
printf("%s", sentence);
return 0;
}
I don't know if I am trying to access restricted memory segment.
Yes, you are trying to modify read-only memory.
You cannot rearrange a constant string.
With a simple change you can fix it:
char sentence[] = "All work and no play makes jack a dull boy";
(use an array instead of a pointer)
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 6 years ago.
I tried the following code with sprintf, but it crashes in some cases and works fine in the other. Could anyone explain it to me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
//char *s = malloc(20); //works fine
//char *s = ""; //does not work, no matter what the initial value is
char s[20]; //works fine
sprintf(s, "%s", "hello world");
printf("%s",s);
return 0;
}
When you are doing this:
char *s = "";
or
char *s = "longer string";
You are creating a literal string which is possibly placed in read-only memory. So you cannot change it later.
If you try to do the following syntax:
char s[] = "...";
the array will be initialized with the literal string and you will be able to modify the original array later.
Suggested questions on site:
What is the difference between char s[] and char *s in C?
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”?
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 9 years ago.
#include <stdio.h>
#include <conio.h>
void test(char *p)
{
p = p + 1;
*p = 'a';
}
int main()
{
char *str = "Hello";
test(str);
printf("%s", str);
getch();
return 0;
}
When I run this code it gives segmentation error ? why is this happening. The const theory is not clear to me... whereas if I declare str as char str[], it does the job. Are they not both basically the same things ?
str is pointing to a string literal, you are attempting to modify the string literal in the function test on this line:
*p = 'a';
It is undefined behavior to attempt to modify a string literal. You can alternatively, make a copy of the string literal into a array as follows:
char str[] = "Hello";
now it is perfectly ok to modify str. From the draft C99 standard, under section 6.4.5 String literals paragraph 6:
It is unspecified whether these arrays are distinct provided their elements have the
appropriate values. If the program attempts to modify such an array, the behavior is
undefined.
*p = 'a';
The problem is the above statement tries to modify the read only segment. String literal "Hello" resides in read only segment and can not be modified.
char str[] = "Hello";
The above statement copies Hello to str character array and the array contents can be modified.