Posted by uhuru on 20:12:00 12-12-2002
if in a function you have:
struct post *mypost;
char address[256];
how do you copy a pointed int menber
(mypost--->codenumber) into an array(address).
The member is an integer
Posted by KaGez on 23:16:00 12-12-2002
erm... lemme ask something first:
why in the world do you wanna copy a integer to a char?! :/
[addsig]
Posted by neonbjb on 06:27:00 12-21-2002
its pretty easy, you just include stdlib.h and call the
sprintf(address, "%d", mypost--->codenumber);
and the number is saved.
Posted by PGuard on 18:21:00 12-21-2002
Hmm That is _not_ the best way to do it! If the number is like: 433335 then you use 6 bytes (+ the NULL) but the size of an int is 4 bytes. And the max value for signed 32 bit is something near -2147483647 to +2147483648.
Code:
address[0]=((char)((unsigned int)mypost--->codenumber-->-->24));
address[1]=((char)((unsigned int)mypost--->codenumber-->-->16));
address[2]=((char)((unsigned int)mypost--->codenumber-->-->8));
address[3]=((char)((unsigned int)mypost--->codenumber-->-->0));
This will allways use 4 bytes. No more, no less. And I think its faster than calling sprintf. (and to do it your self is nicer then just to call some standard library function)
PGuard
Posted by KaGez on 02:02:00 12-22-2002
btw, for those who don't know it:
signed int means that you can store positive and negative values in that variable. Since the average home computer today is 32bit, you can easily calculate the biggest possible, and smallest possible number for a signed integer by doing:
(2^32):2 = 2147483648
You have to do the :2 because you want to use one half of the available memory for the into for negative, and the other half for positive numbers. So the min is -2147483648, and the max is +2147483648.
A unsigned integer means that you can only store positive numbers in the variable. Again, calculating the biggest nuber in it is easy:
2^32 = 4294967296
Well, it's only for those who didn't know it
[addsig]
Posted by PGuard on 08:23:00 12-22-2002
Quote:
On 2002-12-22 02:02, KaGez wrote:
So the min is -2147483648, and the max is +2147483648.
No, you are wrong...min: -2147483648, and max: 2147483647.
PGuard
Posted by KaGez on 22:32:00 12-22-2002
ok, yes, sorry, forgot to add the 0 to the calculations
but still, it gave those who had no clue about it at least a little bit of a clue
[addsig]