Return to Website

C++ / VC++ / MFC Discussion Forum

Welcome to the forum. Now, you can use HTML in your posts too.

C++ / VC++ / MFC Discussion Forum
Start a New Topic 
Author
Comment
View Entire Thread
Re: data types understanding....

Here's the code to convert a string to a ULONG and
vice versa. But please, take a careful look at the
note at the end of the code.

ULONG ulValue;
char szTestString[]="nam";
ulValue =(ULONG)szTestString; // Now ulValue contains "nam"

char szCheckString [ 4 ] ;
memcpy(szCheckString,(void * )ulValue,4); // Now, szCheckString contains "nam"

NOTE : I have used the test string as "nam" and NOT
"name" bcoz char is one byte agreed and ULONG is 4
bytes , agreed. But what about the NULL termination
character. If u use the string "name", its actually
4 characters PLUS one character for NULL. That makes
it 5 CHARACTERS. That cant be possibly put into a
ULONG. It will only lead in buffer overflows and
unpredictable results.

Hope this helps. Have a nice day.

Re: data types understanding....

what about this code?
union a
{
struct b{
char c ;
};
ulong d;
};
union a x;
x.b.c ='n';
x.b.c = 'a';
x.b.c ='m';
x.b.c ='e';
char name[]="name"
for(int i=0; i < strlen(name);i++)
x.b.c = name ;
ulong u = x.d;