PDA

View Full Version : help



gamehunter101
November 7th, 2006, 01:39
i need alittle help with this code



#include <iostream>

using namespace std;

void main()
{
char c = 'a';
char *ptr = &c;

cout << (int)&c <<endl;
cout << (int)*ptr <<endl;

c= 'b';
*ptr = 'd';
cout << 'c' << endl;
}



i get this error



: warning C4311: 'type cast' : pointer truncation from 'char *__w64 ' to 'int'
Compiling manifest to resources...
Linking...


i was following a tut from 3d buzz but they used visual c++ 03 i have the 05 version i dont no if that will make it not work thanks for the help

splodger15
November 7th, 2006, 16:27
I dont know much about c++ but hasnt it all got to be on seperate lines

jak66
November 7th, 2006, 16:40
it probably is splodger, last time i tried using the 'code' tags it put it as a block of code rather than seperate lines

PSPKOR
November 7th, 2006, 17:33
Its fine to put the code where ever you want as long as you have the {} at start and end.

Can you not double click on the error and it should show you where abouts the error is. Cant help no more than that as I've only just started learning myself.

Coder_TimT
November 7th, 2006, 18:57
Looks like your warning is coming from the fact that your are using a 64 bit machine whereas the tutorial was probably originally written for a 32 bit machine.

On your machine the character pointer is 64 bits and the int is still 32 bits, so to get rid of the message cast the pointer to a long int instead of just an int.

cout << (long int)&c <<endl;

Now as for the tutorial in general, I question some of it...maybe it gets built upon to explain things further...

char c = 'a';
char *ptr = &c;
cout << (int)&c <<endl;

/*why are they casting the below to an int? This is the data the pointer actually points to...so they are casting a char to an int...so you get the integer representation of 'a'...take off the typecast and you get 'a' printing out, which makes more sense to me. But like I said, maybe they expound on this and change it later or something...*/
cout << (int)*ptr <<endl;

c= 'b';
*ptr = 'd';
/*And here, this just prints out 'c'. Is this a typo? Anyway remove the quotes to get it to print the value of the variable c which at this time is 'd'.*/
cout << 'c' << endl;