c++ - 而(p = strtok(NULL ,","))警告:possible incorrect assignment

标签 c++ c

有一个代码,但我收到错误

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

那么你能建议我如何处理这个警告/错误吗?

代码:

/* convert csv data to libsvm/svm-light format */

#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 filename\n",argv[0]); }
    if((fp=fopen(argv[1],"r"))==NULL)
    {
        fprintf(stderr,"Can't open input file %s\n",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

关于c++ - 而(p = strtok(NULL ,","))警告:possible incorrect assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20877826/

相关文章:

c++ - 如何从 C++ 函数返回多个值?

c++ - 是首先调用 init 部分函数还是构造函数?

c - Valgrind 错误

c - 返回码误解

c - 在一维数组中动态存储质数

c - C 中简单包装函数的用途

c++ - 如何在 Visual Studio 2012 Express 中更改属性页中的 "Inherited Values"

c++ - 如何将 boost::object_pool 与 std::unique_ptr 一起使用?

c++ - 由于错误代码10093,C++程序无法连接到服务器

c - 交换 2 个 char_type 变量中不同位置的位