解析的输入字符串给出错误的结果

解析的输入字符串给出错误的结果

问题描述:

#include<stdio.h>
#include<string.h>
main()
{
	char* newme(char* input1);
	char newi[100],data[100]="10,10,50,120,20,30,10,140,205";
	strcpy(newi,newme(data));
	printf("%s");
	return 0;
}
char* newme(char* input1)
{
	int Data[20],i=0;
	float A,B,C,D,E,F,G,H,I;
	char seps[]=",";
	char* token;
	char input2[25];
	strcpy(input2,input1);
	printf("%s",input2);
	token=strtok(input2,seps);
	while(token!=NULL)
	{
		Data[i]=atof(token);
		token=strtok(NULL,seps);
		i++;
		printf("%f",Data[i]);
	}
	printf("\n%d",i);
     A=Data[0];B=Data[1];C=Data[2];D=Data[3];E=Data[4];F=Data[5];G=Data[6];H=Data[7];I=Data[8];
	printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
	return input2;
}



输出必须与输入相同,但它给出-1。#INDO表示大部分值



我尝试过:



增加缓冲区大小,将atof更改为atoi


The output must be same as the the input but instead it gives -1.#INDO for most of the values

What I have tried:

Increasing the buffer size, changing atof to atoi

我在这里发布一个现代编译器的输出,其中包含'所有警告激活'选项(即 gcc -Wall )在你的代码上调用:

I post here the output of a modern compiler with the 'all warnings activated' option (namely gcc -Wall) invoked on your code:
foo.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^
foo.c: In function ‘main’:
foo.c:8:9: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
  printf("%s");
         ^
foo.c: In function ‘newme’:
foo.c:23:11: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
   Data[i]=atof(token);
           ^
foo.c:26:10: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
   printf("%f",Data[i]);
          ^
foo.c:30:9: warning: function returns address of local variable [-Wreturn-local-addr]
  return input2;
         ^
foo.c:29:2: warning: ‘I’ is used uninitialized in this function [-Wuninitialized]
  printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
  ^
foo.c:29:2: warning: ‘H’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘G’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘F’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘E’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘D’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]







现在让我们来看看检查上面的混乱......




Now let's examine the above mess...

foo.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^

您应该始终指定 C 函数的返回值的类型(在此你很幸运,因为默认的 int 是正确的。





You should always specify the type of the return value of a C function (in this particular scenario you are lucky because the default, int, is correct).


foo.c: In function ‘main’:
foo.c:8:9: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
  printf("%s");
         ^

你没有提供必需的参数(要打印的字符串)。



You didn't provide the required argument (the string to print).

foo.c: In function ‘newme’:
foo.c:23:11: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
   Data[i]=atof(token);
           ^

您应该包含 stdlib.h ,以便使用 atof



You should include stdlib.h in order to use atof.

foo.c:26:10: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
   printf("%f",Data[i]);
          ^

格式说明符与打印输出的变量类型不匹配(通常需要更改变量类型的格式说明符)。





There is a mismatch between the format specifier and the type of the variable to printout (you usually have to either change the format specifier of the variable type).


foo.c:30:9: warning: function returns address of local variable [-Wreturn-local-addr]
  return input2;
         ^

这应该会让你的头铃响起,因为它通常是传入灾难的第一个迹象:在执行函数后,局部变量不再存在。




This should make ring a bell very loud in your head, because it is usually the first sign of the incoming catastrophe: after function execution, a local variable does not exist any more.


foo.c:29:2: warning: ‘I’ is used uninitialized in this function [-Wuninitialized]
  printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
  ^
foo.c:29:2: warning: ‘H’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘G’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘F’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘E’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘D’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]

打印出未初始化的变量通常会产生奇特的结果。





底线是:编译器是你的朋友,明智地使用它。

Printing out uninitialized variables usually produces fancy results.


The bottom line is: the compiler is your friend, use it wisely.


尝试替换

Try to replace
i++;
printf("%f",Data[i]);



by


by

printf("%f",Data[i]);
i++;



它可能会有所帮助



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - *,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。


it may help

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.