J
Jess
Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
int main(){
char* x[] = {"a","b","c"};
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
*(x[0]) = 'A'; //since string "a" is an array of chars, and *(x[0])
is the location in memory that stores the first char in the array, ie
'a'. this line of code tries to change it to 'A'
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
return 0;
}
The output is:
a
b
c
Segmentation fault
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
int main(){
char* x[] = {"a","b","c"};
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
*(x[0]) = 'A'; //since string "a" is an array of chars, and *(x[0])
is the location in memory that stores the first char in the array, ie
'a'. this line of code tries to change it to 'A'
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
return 0;
}
The output is:
a
b
c
Segmentation fault