printf((char *)&num);
is clearly an error; you are casting a double* to a char* (which is incorrect). unpleasant consequences would be the norm if you do such things. in C, to print out a double value, use
printf( "%f", num ) ;
instead.
atof is to be shunned; it has many problems. you would be much better off (and less likely to make errors) if you use C++ facilities where they are available. eg.
#include <string>
#include <iostream>
#include <sstream>
using namespace std ;
/*void*/ int main()
{
double num = 310.589 ;
/* char buffer [10]; */ string buffer ;
/* sprintf(buffer,"%f",num); */
{ ostringstream stm ; stm << num ; buffer = stm.str() ; }
// buffer[7] = *NULL;
/*
printf("using &num=\n");
printf((char *)&num);
printf("\nbuffer=\n");
printf(buffer);
printf("\nfragment of buffer=\n");
printf((char *)&buffer[3]);
*/
cout << "using &num= " << &num << "\nbuffer= " << buffer
<< "\nfragment of buffer= " << &( buffer.c_str()[3] ) << '\n' ;
/* scanf("%s",buffer); */ cin >> buffer ;
/* num=atof(buffer); */
{
istringstream stm(buffer) ;
stm >> num ;
if( !stm ) cerr << "conversion failed\n" ;
}
num/=2;
/* printf("\n%lf",num); */ cout << num << '\n' ;
/* _getch(); */ char ch ; cin >> ch ;
} if you are preparing yourself for c++0x (you really should; final draft should be out in a few months), you could write
#include <string>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
using namespace std ;
using namespace boost ;
int main()
{
double num = 310.589 ;
string buffer = lexical_cast<string>(num) ;
cout << "using &num= " << &num << "\nbuffer= " << buffer
<< "\nfragment of buffer= " << &( buffer.c_str()[3] ) << '\n' ;
cin >> buffer ;
try { num = lexical_cast<double>(buffer) ; }
catch( const bad_lexical_cast& ) { cerr << "cast error\n" ; }
num/=2;
cout << num << '\n' ;
char ch ; cin >> ch ;
} note: lexical_cast used here is from boost; it would be part of c++0x