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.
i have written a code . this looks as follows .
#include<stdio.h>
#include<string.h>
int do_print2(char *q[]);
int main()
{
char *p[]={"pointervaule","NAM", "JAM", "CALM"};
do_print2(p);
return 1;
}
int do_print2(char *p[])
{
printf("this is print1 char *p \n");
strcat(p[0],"added");
printf("%s\n", (p[0]));
return 1;
}
after compilation, i am trying to run it, i am getting segmentation fault. help me in learning what is the reason for that error. thank in advance.
In your code: strcat(p[0],"added"); try to write on read only memory that is illegal in C. Because p[0] points to a constant string.
Not p is pointer to char array, but not 2-dimensional char array.
Read: Difference between char* str[] and char str[][] and how both stores in memory? an answer with diagrams and code examples, to understand it better.
The OS says that the C strings are in the read section of the object (i.e. protected from writing).
Due to historical reasons "bla bla" is really a const char * const data type, but is allowed to get away in the C compilers eyes some teenage interdependencies. But the headmaster (OS) is less forgiving and expels the running of such code in the corridors. (how many metaphors in that statement).
You can't write to read only memory, better way to do this:
#include<stdio.h>
#include<string.h>
int do_print2(char q[][20]);
int main()
{
char p[4][20] = {{0}, {0}, {0}, {0}};
strcat(p[0],"pointervaule");
strcat(p[1],"NAM");
strcat(p[2],"JAM");
strcat(p[3],"CALM");
do_print2(p);
return 1;
}
int do_print2(char p[][20])
{
printf("this is print1 char *p \n");
strcat(p[0],"added");
printf("%s\n", (p[0]));
return 1;
}
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 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)
Why is this string reversal C code causing a segmentation fault? [duplicate]
(8 answers)
Closed 5 years ago.
#include<stdio.h>
#define ASIZE 50
void Reverse(char *str){
int Asize,i=0;
char temp;
// Find the length of the string
while(*(str+i)!='\0'){
i++;
}
Asize=i;
// string reverse
for(i=0;i<(Asize/2);i++){
temp = *(str+i);
//may be below is some error with first input method 1
//but for input method 2 it works perfectly
*(str+i) = *(str+(Asize-(i+1)));
*(str+(Asize-(i+1))) = temp;
}
}
int main()
{
//input method 1. (error aries while i pass the pointer as argument)
char *s = "abcxyz";
//input method 2 (works perfectly while as function parameter)
char s[ASIZE];
scanf("%s",s);
Reverse(s);
printf("%s",s);
}
In the main the input method 1 not working perfectly for the reverse of the string, but the method 2 works.
The concept of mine is not clear with the memory representation of char pointer. Maybe I am not good to make the question correctly but Would someone please make me clear why the method 1 is not working. Thanks in advance for the help.
"abcxyz" is actually a const char[7] type that can decay to a const char* in certain situations.
It is not a char* type; the behaviour on attempting to modify the string is undefined.
char s[ASIZE]; on the other hand is an array of char with automatic storage duration. You are free to modify any element of that as you please.
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 7 years ago.
I am trying to write a simple encryption program (Caesar cipher) and I am running into a snag. I am relatively new to the world of C and pointers, coming from Java originally.
Whenever I run the following code, it will give me the error message Segmentation fault and terminate.
I have done a little reading about what that means, but I still don't fully understand it, or what is wrong with my code, or how to remedy the issue.
If you could help with any of those things that would be greatly appreciated.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void encrypt(char *input);
int main()
{
char *instructions = "pipi";
encrypt(instructions);
printf("Here are your secret instructions:\n%s\n", instructions);
return (0);
}
void encrypt(char *input) {
while (*input != '\0') {
if (isalpha(*input)) {
*input += 1;
if (!isalpha(*input)) {
*input -= 26;
}
}
input++;
}
}
String literals in C, like "pipi" are read only, trying to modify such a string will lead to undefined behavior.
Use an array if you want to modify the string:
char instructions[] = "pipi";
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)
Segmentation fault (core dumped) in a simple C code
(3 answers)
Closed 8 years ago.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *Change(char *str,int start,int end)
{
if(start==end || start>end){
return str;
}
else{
char temp=str[start];
str[start]=str[end];
str[end]=temp;
return(Change(str,start++,end--));
}
return str;
}
char *Reverse(char *str)
{
int length=strlen(str);
return(Change(str,0,length-1));
}
int main()
{
printf("%s\n",Reverse("program"));
return 0;
}
I am trying to write a recursive function to reverse a string,but it comes the linking error.Please help.I had tried so many times and searched in the Internet,but it can't help.
I guess the most probably place causing the problem is in function Change,but I can't solve with it.
You're passing a string literal (which is a const char*) to Reverse(), since you can't modify a string literal, your program generates a segmentation fault.
You'll either need to pass a modifiable string to Reverse():
char myStr[] = "program";
printf("%s\n",Reverse(myStr));
Or you can make a copy of the input string in Reverse():
char *Reverse(const char *str)
{
int length=strlen(str);
char* temp = malloc(length);
strcpy(temp, str);
return(Change(temp,0,length-1));
}
In that case, you'll need to free the string returned by Reverse():
char* reverseStr = Reverse("program");
printf("%s\n", reverseStr );
free(reverseStr);
Also in change(), start++ end end-- will return the value before they are incremented, you need to use either ++start or simply start+1 since you won't use those variables anymore:
return(Change(str,start+1,end-1));
Simple explanation: You can't modify a string literal. One more thing is that the statement
return(Change(temp,0,length-1));
will cause compilation error because neither temp is defined in the function Reverse nor it is globally defined.