Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C#
<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>

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
Posted
Updated 9-Sep-16 13:27pm
v2
Comments
[no name] 9-Sep-16 11:34am    
Learn to use your debugger and you will probably find out yourself. I am not even going to try and make sense of that code mess.
Erebus_22 9-Sep-16 12:23pm    
The debugger shows the correct value but the print function isn't printing the correct value, Why is that
[no name] 9-Sep-16 14:39pm    
Actually go back and run your code in the debugger and find out. I am not going to try and debug your code for you. I would have to spend too much time just trying to get it to compile so it would run.

 
Share this answer
 
Comments
Erebus_22 9-Sep-16 12:09pm    
The debugger shows the correct value of the data variables but its the print function which doesn't print the correct value
Patrice T 9-Sep-16 12:36pm    
Yes, you tell printf to print floats and you give integers.
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.

 
Share this answer
 
Try this code

C++
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) ;
}


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().

:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900