Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   choosen random value (http://www.go4expert.com/forums/choosen-random-value-t25181/)

fulla 8Mar2011 03:42

choosen random value
 
hi..

if I want choose a random value that have 0 or 1 just

HTML Code:

rand() & 1;
is it correct ??

thanks..

xpi0t0s 8Mar2011 23:20

Re: choosen random value
 
What happens if you try it?

fulla 9Mar2011 18:05

Re: choosen random value
 
its work good but relly i dont understand whay !!

I have aprublem ..

i declerat apointer to pointer variable..

then I wan but this variable=null;
I got error I dont know whay??

ex:
int **T;
*T= NULL;

is this an error ??

xpi0t0s 9Mar2011 20:26

Re: choosen random value
 
The rand() works because & is a bitwise operator, so &1 just takes the least significant bit. Look at the binary for the return value; if the results of rand() are, say, 10,5,3,17,6,4,21 then the LSB for those results are 0,1,1,1,0,0,1.

*T=NULL is an error because you haven't defined *T anywhere. When you create a pointer, you allocate space for the pointer itself but you don't allocate space for what that pointer points to. So when you write to *T, after defining just int **T, you're writing NULL to an unknown location in memory.

So if you do:
int T;
int *pT=&T;
int **ppT=&pt;

THEN you can write to T, *pT and **ppT.


All times are GMT +5.5. The time now is 05:15.