#include <stdio.h>
void main(){
char *str[]={"aa","bb"};
str[0][0]='h';
}
I receive a segmentation fault when executing this code. Does anyone know the reason?
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are assigning to a location occupied by a string constant, an undefined behavior. If you know the max lengths of your strings, you can do this:
It’s not a precise equivalent, but it should work. If you do not know max length, or do not want to waste a few bytes here and there, you can still pull it off with a little more work: