值为空但赋值显示值 [英] Value empty but assignment shows a value

查看:72
本文介绍了值为空但赋值显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre lang="C#">#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;string.h&gt;
main()
{
	char* newme(char* input1);
	char newi[100],data[100]=&quot;10,10,5,2,2,3,1,14,25&quot;;
	strcpy(newi,newme(data));
	//printf(&quot;%s&quot;);
	return 0;
}
char* newme(char* input1)
{
	int Data[10],i=0;
	float A,B,C,D,E,F,G,H,I;
	char seps[]=&quot;,&quot;;
	char* token;
	char input2[25];
	strcpy(input2,input1);
	printf(&quot;%s&quot;,input2);
	token=strtok(input2,seps);
	while(token!=NULL)
	{
		Data[i]=atoi(token);
		token=strtok(NULL,seps);		
		printf(&quot;%f&quot;,i)
		;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(&quot;%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f&quot;,Data[0],Data[1],Data[2],Data[3],Data[4],Data[6],A,B,C,D,E,F,G,H,I)
	
	;return input2;
}</pre>



数据数组的输出为0.000,但字母'A-I'的输出是传递给函数的值。

这里Data []值在打印时不包含任何值,但当分配给其他变量包含值时,为什么会这样?

提前谢谢



我尝试了什么:



数据类型已更改,但结果仍然相同


The output of Data array is 0.000 but the output of the letters 'A-I' is the value passed to the function.
Here Data[] values when printed don't contain any values but when assigned to other variables contain a value, Why is that ?
Thanks in advance

What I have tried:

Data types were changed but the result was still the same

推荐答案

你需要正确学习C语言。

以下是该语言作者对C语言参考书的链接。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook。 pdf [ ^ ]
You need to learn C properly.
Here is links to references books on C by the authors of the language.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


您需要了解实际上内存中的所有数据都是相同类型的信息。无论是字符串,整数,浮点数等,都是数据的解释方式。如果你知道什么是铸造那么你应该知道这一点。



现在,当你想打印一个值时,让它说它是一个整数,全部这些位被视为简单的二进制解释,只需添加(如果为1)或忽略(如果为0)。如果你告诉系统将它视为一个浮点数,那么它必须将一些值视为10的幂,尽管它也存储为二进制。



此外,对于不同的数据类型,数据的长度可能不同,如果您使用了错误的类型,这也会将结果更改为意外值。



值30h可以解释为48(一个字节),空格字符(作为字符),指向内存开头的字符串(可能打印程序崩溃或数百页垃圾),等等on。



关于C最美妙的事情之一就是你可以把任何东西当作任何东西 - 但是这种力量来自责任。



理解和经验最终将帮助您完成此任务。

You need to understand that in reality all of the data in memory is the same type of information. Whether it's a string, an integer, a float, etc. is how the data is interpreted. If you know what "casting" is then you should be aware of this.

Now, when you wish to print a value, let say it's an integer, all of the bits are treated as simple binary for interpretation and they are simply added (if 1) or ignored (if 0). If you tell the system to treat it like a float, then it must consider some of the values as the powers-of ten, although it too is stored as binary.

In addition, the length of the data may be different for different data types and this, too will change your results to unexpected values if you use the wrong type.

A value of 30h could be interpreted as 48 (a byte), the space character (as char), a pointer to the start of memory as a string (possible crash of program or hundreds of pages of junk may be printed), and so on.

One of the most beautiful things about C is that you can treat anything as anything - but with this power comes responsibility.

Understanding and Experience will eventually get you through this.


试用此代码



Try this code

char * newme (char * input1)
{
	int       Data [10] ;
     int       i = 0 ;
	float     A,B,C,D,E,F,G,H,I ;
	char      seps [] = "," ;
	char *    token ;
	char      input2 [25] ;

	lstrcpy (input2, input1) ;
	printf ("%s\n", input2) ;
	token = strtok (input2, seps) ;
	while (token != NULL)
	{
		Data [i] = atoi (token) ;
		token = strtok (NULL, seps) ;		
		printf ("%d) %d\n", i, Data [i]) ;
		i ++ ;
	}
     printf ("\n") ;
	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] ;

     for (i = 0 ; i < 9 ; i ++)
	     printf ("%d) %d\n", i, Data[i]) ;

	printf ("A=%f\n", A) ;
	printf ("B=%f\n", B) ;
	printf ("C=%f\n", C) ;
	printf ("D=%f\n", D) ;
	printf ("E=%f\n", E) ;
	printf ("F=%f\n", F) ;
	printf ("G=%f\n", G) ;
	printf ("H=%f\n", H) ;
	printf ("I=%f\n", I) ;
	return (input2) ;
}





您遇到的问题是尝试使用%f显示整数,这是浮点数!



%d可用于整数。



搜索printf format以获取有关如何使用整数显示数据的详细信息printf()。



:)



The problem you were having is trying to display an integer using %f, that's for floats!

%d can be for integers.

Search for "printf format" for details on how to display data using printf().

:)


这篇关于值为空但赋值显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆