gpt4 book ai didi

c - 传递 'atoi' 的参数 1 使指针来自整数,无需在 C 中进行强制转换

转载 作者:行者123 更新时间:2023-11-30 14:32:48 27 4
gpt4 key购买 nike

我正在尝试从 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 解析为 int。但是,atoi 需要一个 C 字符串(const char*),因此您不能向其传递普通字符。

代替atoi,这可能就足够了(对于单位数字整数):

grelha[l][c] = texto[cont][tam] - '0';

如果您正在考虑解析多位整数,那么您不应该使用 char[][],因为根据您的用法,它们每个条目只能包含一个字符(因此是一位数字)。

关于c - 传递 'atoi' 的参数 1 使指针来自整数,无需在 C 中进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com