I was checking direct comparison between strings using ==
operator. When both string is character pointer that it compare correctly. Sooner I realize it happen compiler by default assign same address to both char pointer variable as they hold the same value.
#include<stdio.h>
void update(char *str2){
*(str2+2)='O';
}
int main(){
char *str1="Sudhanshu";
char *str2="Sudhanshu";
printf(" %s , %s ",str1,str2);
update(str2);
printf(" %s , %s ",str1,str2);
if(str1==str2){
printf("True\n");
}else
printf("False\n");
return 0;
}
The address of str1
and str2
is the same. So I want to check whether updating one pointer actually affect other.
However, I'm getting a segmentation fault. Why am I getting a seg fault?