您的位置:首页 > 运维架构

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

2013-02-24 15:49 411 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐