c - 为什么在更改循环条件时会出现分段错误?
问题描述
我正在学习链接列表,并且在构建它的循环时遇到了麻烦。这是我的导致分段错误的代码。
struct node {char data[20]; struct node *next;};
struct node *head = NULL, *newNode = NULL, *p;
char temp[20];
while (temp[0] != 0)
{
temp[0] = 0;
printf("Enter a word: ");
scanf("%s", temp);
newNode = malloc(sizeof(struct node));
strcpy(newNode->data, temp);
newNode->next = head;
head = newNode;
}
当我用它替换temp[0] != 0
它temp[0] == 0
时不会导致分段错误。这里发生了什么?为什么相同的检查倒置不起作用?
解决方案
推荐阅读
- javascript - Browsersync:拒绝执行来自 'XX/app.js' 的脚本,因为它的 MIME 类型('text/html')不可执行等
- javascript - 使用 React 访问嵌套的 json
- amazon-s3 - 当 AEM 配置为使用 S3 数据存储时,它会加快蓝绿部署吗?
- installation - ICP 2.1.0.3 企业安装无法解析 IP 名称/主机名
- django - Django:预填充表单集
- javascript - 在 JavaScript 中的新变量之前不写“var”
- javascript - 替换 HTML 内容停止了其他 jquery 事件
- javascript - semver中的Node.js正则表达式非捕获组
- reactjs - Action Creator 和 Reducer 不是 console.logging,但组件是连接的
- scala - 如何将配置文件添加到以 YARN-CLUSTER 模式运行的 Spark 作业?