converting arguement to unsigned int 
Author Message
 converting arguement to unsigned int

Hey guys, here is a snippet from some code I created (bare in mind that I
have removed parts that check for no input and this is mearly here to ask a
specific question):

int main(int argc, char **argv)
{
    char *arg = argv[2];
    unsigned int argn = (char)arg;
    srand(argn);

Quote:
}

This makes it possible to use anything as a 'seed' for srand(). Note that
accuracy (that is, argn being the exact number inputted) isn't required
since the same arguement yeilds the same result.

A couple of things I am wondering. Is there a better way to do this? Is this
legal in ANSI-C (it compiles flawlessly in LCC)? Is this procedure undefined
behavior? (I do not know the C specification nor do I know where to get it.)

And lastly, am I correct in saying that the element of the pointer argv
array is a string? If so, then it is completely legal, and even ideal (in
this case), to cast char to a string array pointer. ;-)

Thanks in advance, and yeah, I am a newbie. (That might explain why I'm not
making sense.) :-)

Josh
--



Fri, 30 May 2003 02:28:16 GMT  
 converting arguement to unsigned int
Quote:

> Hey guys, here is a snippet from some code I created (bare in mind that I
> have removed parts that check for no input and this is mearly here to ask a
> specific question):

> int main(int argc, char **argv)
> {
>     char *arg = argv[2];
>     unsigned int argn = (char)arg;
>     srand(argn);
> }

> This makes it possible to use anything as a 'seed' for srand(). Note that
> accuracy (that is, argn being the exact number inputted) isn't required
> since the same arguement yeilds the same result.

> A couple of things I am wondering. Is there a better way to do this? Is this
> legal in ANSI-C (it compiles flawlessly in LCC)? Is this procedure undefined
> behavior? (I do not know the C specification nor do I know where to get it.)

> And lastly, am I correct in saying that the element of the pointer argv
> array is a string? If so, then it is completely legal, and even ideal (in
> this case), to cast char to a string array pointer. ;-)

> Thanks in advance, and yeah, I am a newbie. (That might explain why I'm not
> making sense.) :-)

> Josh
> --


I suspect that for each os argv will point to similar area of memory since the data
passed as arguments to main mostly located on the stack!
--

Patzschke + Rasp Software AG                        http://www.prs.de
Bierstadter Stra?e 7                          Fax: +49-(0)611-1731-31
D-65189 Wiesbaden       Phone: +49-(0)611-1731-611/+49-(0)173-9405050
--



Sat, 31 May 2003 11:53:53 GMT  
 converting arguement to unsigned int

Quote:

> int main(int argc, char **argv)
> {
>     char *arg = argv[2];
>     unsigned int argn = (char)arg;
>     srand(argn);
> }

This program can't be what you intended.  Your first statement in main
assigns a char to a char *, which is a type mismatch.  The second takes
a char *, casts it to a char, and then implicitly casts that to an
unsigned int.  There's no guarantee that char *, char, and unsigned int
have even remotely similar sizes, so there's lots of truncation and
extension going on here.

Furthermore, I can't discern what your intent was.  Did you mean to use
the address of argv + 2 as a seed, or did you mean to use the character
at argv[2] as a seed?  Neither would be very good seeds.

Quote:
> A couple of things I am wondering. Is there a better way to do this?

The canonical way is:

    #include <stdlib.h>
    #include <time.h>

    srand((unsigned int) time(0));

Quote:
> Is this
> legal in ANSI-C (it compiles flawlessly in LCC)?

Try turning on all compiler warnings.  Your compiler should be
complaining about that first line.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ An ounce of hypocrisy is worth a pound of ambition.
\__/ Michael Korda
    Kepler's laws / http://www.alcyone.com/max/physics/kepler/
 A proof of Kepler's laws.
--



Sat, 31 May 2003 11:54:07 GMT  
 converting arguement to unsigned int

Quote:

> int main(int argc, char **argv)
> {
>     char *arg = argv[2];
>     unsigned int argn = (char)arg;
>     srand(argn);
> }
> This makes it possible to use anything as a 'seed' for srand().

Not really, because you're just using an integer that is
more or less the value of the pointer argv[2], not a
conversion of the pointed-to argument string to an
integer (see strtoul).
--



Sat, 31 May 2003 11:56:31 GMT  
 converting arguement to unsigned int


Quote:

> > int main(int argc, char **argv)
> > {
> >     char *arg = argv[2];
> >     unsigned int argn = (char)arg;
> >     srand(argn);
> > }

> This program can't be what you intended.  Your first statement in main
> assigns a char to a char *, which is a type mismatch.  

No it is not.
 argv has type char **.
so argv[2] has type char *.  He is assigning it to a char * not a mismatch.

Roger....
--



Sun, 01 Jun 2003 03:49:42 GMT  
 converting arguement to unsigned int

Quote:

>  argv has type char **.
> so argv[2] has type char *.  He is assigning it to a char * not a
> mismatch.

Sorry, totally blanked out on that one there.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Awards are merely the badges of mediocrity.
\__/ Charles Ives
    blackgirl international / http://www.blackgirl.org/
 The Internet resource for black women.
--



Mon, 02 Jun 2003 15:15:12 GMT  
 converting arguement to unsigned int


Quote:

>> int main(int argc, char **argv)
>> {
>> char *arg = argv[2];
>> unsigned int argn = (char)arg;
>> srand(argn);
>> }

Erik> This program can't be what you intended.  Your first statement
Erik> in main assigns a char to a char *, which is a type mismatch.

Looks okay to me - it's assigning char * to char *.

Erik> The second takes a char *, casts it to a char, and then implicitly
Erik> casts that to an unsigned int.
Erik>
Erik> Furthermore, I can't discern what your intent was.

[consults crystal ball]

I think the O.P. meant to convert a supplied (string) number to an
arithmetic type.  strtoul() (from stdlib.h) is the missing detail.
--



Mon, 02 Jun 2003 15:16:40 GMT  
 converting arguement to unsigned int

Quote:

> Hey guys, here is a snippet from some code I created (bare in mind that I
> have removed parts that check for no input and this is mearly here to ask a
> specific question):
> int main(int argc, char **argv)
> {
>     char *arg = argv[2];
>     unsigned int argn = (char)arg;
>     srand(argn);
> }

After reading the responses to this question, which make good arguments,
I was struck by the fact that this code uses argv[2].  Now I know it's
just a snippet, but I felt it should be pointed out that, as in all C
arrays, argv starts at zero (zero being the name of the executable) and
argv[2] is the second parameter passed to that executable.  I don't know
if Josh intended to have a parameter before the one he wanted to seed
srand(unsigned int) with or not - but he should be conscious that two
arguments need to be passed to the executable with the second one (or
its address) being the seed.

Jeff Turner
--



Sun, 08 Jun 2003 06:54:44 GMT  
 converting arguement to unsigned int

Quote:


> >> int main(int argc, char **argv)
> >> {
> >> char *arg = argv[2];
> >> unsigned int argn = (char)arg;
> >> srand(argn);
> >> }
> Erik> The second takes a char *, casts it to a char, and then implicitly
> Erik> casts that to an unsigned int.
> Erik>
> Erik> Furthermore, I can't discern what your intent was.

> [consults crystal ball]

> I think the O.P. meant to convert a supplied (string) number to an
> arithmetic type.  strtoul() (from stdlib.h) is the missing detail.

        This is entirely possible.  Can I get the source code for your
compiler's crystal ball?  Thanks.

--Jeff Turner
--



Sun, 08 Jun 2003 06:55:16 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. converting unsigned int to signed int

2. Convert (int) to (unsigned int)

3. *unsigned int = unsigned int bit: 1;

4. bytes to unsigned char, unsigned short, unsigned int, ...

5. Converting int to unsigned char *.

6. convert string to unsigned int

7. (unsigned int)(unsigned long)(void *)p

8. From unsigned int to unsigned char

9. unsigned char assignment to unsigned int.

10. Unsigned character conversion to (unsigned)int.

11. unsigned int and int

12. int promotion to unsigned int

 

 
Powered by phpBB® Forum Software