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

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;
}

發佈了28 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章