char * から int へのキャストは精度を失います 質問する

char * から int へのキャストは精度を失います 質問する

ファイルから数字を読み取っています。各数字を2次元配列に入れようとすると、以下のエラーが発生します。このメッセージを取り除くにはどうしたらよいでしょうか? 変数: FILE *fp; char line[80];

エラー: char * から int へのキャストにより精度が失われます

コード:-

#include<stdio.h>
#include<string.h>

int main()
{
        FILE *fp;
        char line[80],*pch;
        int points[1000][10];
        int centroid[1000][10];
        float distance[1000][10];
        int noofpts=0,noofvar=0,noofcentroids=0;
        int i=0,j=0,k;

        fp=fopen("kmeans.dat","r");
        while(fgets(line,80,fp)!=NULL)
        {
                j=0;
                pch=strtok(line,",");
                while(pch!=NULL)
                {
                        points[i][j]=(int)pch;
                        pch=strtok(NULL,",");
                        noofvar++;
                        j++;
                }
                noofpts++;
                i++;
        }
        noofvar=noofvar/noofpts;
        printf("No of points-%d\n",noofpts);
        printf("No of variables-%d\n",noofvar);

        return 0;
}

ベストアンサー1

問題となっている行は次のとおりです:

points[i][j]=(int)pch;

これを次のように置き換える必要があります

points[i][j]=atoi(pch);

atoiは、10 進数表現の整数を表す C 文字列を に変換する関数ですint

おすすめ記事