而 (p=strtok(NULL, "," )) 警告:p可识别的错误分配
while (p=strtok(NULL,",")) warning :possible incorrect assignment
有一个代码,我收到错误
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 中使用 malloc
和 free
对两个数组进行动态分配,或者在 C++ 中使用std::vector
。
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 提升 ASIO 无法识别计时器对象
- 函数中堆分配的效果与缺少堆分配的情况
- 从udp接收帧对于人脸识别来说太慢
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 模板类无法识别友元运算符
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 如何静态识别动态堆分配?
- 识别矩阵变量分配的行为
- 而 (p=strtok(NULL, "," )) 警告:p可识别的错误分配
- Delete无法识别列表中的指针分配
- Visual Studio 2010 + CUDA 4:尝试使用NPP分配内存时出现无法识别的令牌错误