[Segmentation Fault] Oops! The filename points to a constant string

来源:互联网 发布:zsy网络用语是什么意思 编辑:程序博客网 时间:2024/04/29 20:09

We can compile the following program using the gcc compiler. 

But when running it, we get a segmentation fault.

Why ?

#include <stdio.h>int main(){  // The following statement is equal to " constant char* filename = "file1.txt"; "in c++  char* filename = "file1.txt"; // Initialization and after that filename points to a constant string.  printf("\nPlease input for the filename ==> ");  scanf("%s", filename); // Try to modify a constant string. It will cause a segmentation fault !  printf("\nThe file name you have input is %s", filename);  return 0;}