而 (p=strtok(NULL, "," )) 警告:p可识别的错误分配

while (p=strtok(NULL,",")) warning :possible incorrect assignment

本文关键字:识别 分配 错误 警告 strtok NULL      更新时间:2023-10-16

有一个代码,我收到错误

Line 8: array size too large
line 9: array size too large 
line 28 while (p=strtok(NULL,",")) warning :possible incorrect assignment 

那么,您能建议我如何处理此警告/错误吗?

法典:

/* 将 CSV 数据转换为 libsvm/svm-light 格式 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[10000000];
float feature[100000];
int main(int argc, char **argv)
{
FILE *fp;
    if(argc!=2) { fprintf(stderr,"Usage %s filenamen",argv[0]); }
    if((fp=fopen(argv[1],"r"))==NULL)
    {
        fprintf(stderr,"Can't open input file %sn",argv[1]);
    }
    while(fscanf(fp,"%[^n]n",buf)==1)
    {
        int i=0,j;
        char *p=strtok(buf,",");
        feature[i++]=atof(p);
        while((p=strtok(NULL,",")))
            feature[i++]=atof(p);
        //      --i;
        /*
        if ((int) feature[i]==1)
        printf("-1 ");
    else
        printf("+1 ");
       */
       //       printf("%f ", feature[1]);
        printf("%d ", (int) feature[0]);
        for(j=1;j<i;j++)
        printf(" %d:%f",j,feature[j]);

        printf("n");
    }
    return 0;
}

您实际上希望将从strtok返回的值存储到变量p中,同时检查该值是否为NULL。

我建议你用以下方式写它,这是等效的,与目前的写法相比更具描述性。

while( ( p=strtok(NULL,",") )!=NULL )

关于数组大小太大的错误,应考虑在 C 中使用 mallocfree 对两个数组进行动态分配,或者在 C++ 中使用std::vector