c - 传递 'atoi' 的参数 1 从整数中生成指针,而无需在 C 中进行强制转换
问题描述
我试图从 txt 文件中输入这些数字:
1 1 1 1
2 2 2 2
1 1 1 1
在具有此代码的一个整数数组中:
char texto[Max_Linhas][Tamanho_Linha];
//int tamanhodagrelha
int i=0,tamanhodagrelha, cont, tam, NumLinhas, l, c;
int grelha[Max_Linhas][Max_Linhas];
fch = fopen("ficheiro.txt", "r");
if (fch == NULL)
{
printf("O arquivo não foi aberto.\n");
}
//Lê conjunto de caracteres ate \n ou \t
fscanf(fch, "%d", &tamanhodagrelha);
NumLinhas = (tamanhodagrelha+2);
while( i<NumLinhas && fgets(texto[i],Tamanho_Linha,fch))
{
i++;
}
for (cont=1; cont<= tamanhodagrelha; cont++)
{
tam=0;
l=0;
while( texto[cont][tam] != '\n')
{
if(texto[cont][tam] != ' ')
{
for(c=0; c< tamanhodagrelha; c++)
{
grelha[l][c] = atoi(texto[cont][tam]);
}
}
tam++;
}
l++;
printf("\n");
}
但是我在“grelha[l][c] = atoi(texto[cont][tam]);”行中有这个错误(传递 'atoi' 的参数 1 使指针从整数而不进行强制转换)我不知道该怎么办。
解决方案
您似乎正在尝试解析从char
to的个位数整数int
。但是,atoi
需要一个 C 字符串 (a const char*
),因此您不能将其传递给纯字符。
而不是atoi
,这可能就足够了(对于单个数字整数):
grelha[l][c] = texto[cont][tam] - '0';
如果您正在考虑解析多位整数,那么您不应该使用 char[][],因为根据您的使用情况,每个条目只能包含一个字符(因此只有一个数字)。
推荐阅读
- python - Keras 验证生成器评估指标
- php - 使用私有存储桶授权的 Backblaze Storage B2 PHP 下载
- python - 添加 webdriverwait 功能以加载 href 页面时,Selenium 拒绝连接
- jquery - jQuery slideToggle 使隐藏内容开始和结束BEHIND clicked div
- linux - 通过脚本迭代目录的内容
- node.js - 从数组 mongodb 中查找前 3 个值
- dynamics-crm - 即使具有组织级别的读取权限,查看记录时也会出现权限错误
- angular - Angular 6 单例服务总是在 APP_INITIALIZER 之后重置
- corda - Cordapp 教程在 Fedora VirtualBox 机器中崩溃
- php - 使用 ImageMagick 时如何修复“无法注释图像”错误?