Mail Archives: djgpp/1998/07/27/07:30:23
Erik Max Francis (max AT alcyone DOT com) wrote:
> Nicklas Lindgren wrote:
> > I've tried making a pointer to a char in the string, and then
> > implicitly converting it to an int. I used something like:
> >
[Nicklas' example proggie deleted]
> >
> > The same number comes out in the other end, but it doesn't seem to end
> > up in the string.
>
> Of course it doesn't. ints and NUL-terminated character strings are not
> the same thing.
That is very true. If you _really_ want to do it, try a union of an
int and a string. Take a basic_learn_how_to_C-book and read the union
section.
The example program at the end, compiled and run on SUN unix
machines the program at the end gives as output:
0x12 0x34 0x56 0x78 0x0
0x0 0x34 0x56 0x78 0x0
It will probably be reversed on a PC.
hth,
Robert
====== start example prog ======
#include <stdio.h>
#define IS sizeof(int)
union xx {
int jan;
char nix[IS+1];
};
union xx xxx;
int main() {
unsigned i;
xxx.nix[IS]='\0';
xxx.jan = 0x12345678;
for (i=0; i < IS+1; i++) printf("0x%x ", xxx.nix[i]);
printf("\n");
xxx.jan = 0x345678;
for (i=0; i < IS+1; i++) printf("0x%x ", xxx.nix[i]);
printf("\n");
return 0;
}
==== end example prog ====
--
rjvdboon AT cs DOT vu DOT nl | "En dat is niet waar!" sprak (ex?) Staatsecre-
www.cs.vu.nl/~rjvdboon | taris Netelenbos (onderwijs) fel.
- Raw text -