c - ROT13 实现中的分段错误和警告
问题描述
我正在编写一个实现 rot13 的函数,我只制作 a,b,...,m (+13) 的情况,但我有一个分段错误错误和警告:
代码:
#include <stdio.h>
#include <string.h>
char rot13(char palabra[]) { //char *palabra
int y = (short) strlen(palabra);
char abc[27]="abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < y ; ++i) {
if(palabra[i]<'m' && palabra[i]>='a'){
for (int j = 0; j <26 ; ++j) {
if (palabra[i]==abc[j]){
palabra[i]=abc[j+13];
}
}
}
}
return palabra;
}
int main() {
rot13("aaa");
return 0;
}
警告:
main.c:18:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
return palabra;
我需要帮助来理解警告以及如何解决问题,谢谢!
解决方案
Another major problem is that your code is trying to change a string literal (the "aaa"
) which is considered undefined behavior in C. Compilers are free to store string literals in a read-only memory region.
It's doing it in palabra[i]=abc[j+13];
, where you overwrite each byte of the input parameter.
推荐阅读
- javascript - Bing API 在欧洲并不总是返回相同的信息(邮政编码等)
- vb.net - 如何使用 vb.net 从 Docusign api 下载文件
- git - 允许用户仅合并特定分支的最佳方法是什么?
- reactjs - 在 React 中传递一个道具与多个道具与嵌套组件?
- html - 无法展开二级菜单
- sql - 如何计算具有 NULL 值的特定列的不同行是所有行
- wordpress - Wordpress 自定义 url 重写 htaccess
- python-3.x - 为什么在使用 BERT 的 learner.autofit 期间数据项会发生变化?
- python - xgboost 中的分类特征
- javascript - 使用 JavaScript 迭代表列