首页 > 解决方案 > 传递 '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 使指针从整数而不进行强制转换)我不知道该怎么办。

标签: c

解决方案


您似乎正在尝试解析从charto的个位数整数int。但是,atoi需要一个 C 字符串 (a const char*),因此您不能将其传递给纯字符。

而不是atoi,这可能就足够了(对于单个数字整数):

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

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


推荐阅读