-2

In C, why does

strtoul(argv[1])

just doesn't work? It looks like more parameters are needed but I can't prevent how long the number will be.

Thanks!

p.s. (argv[1] is properly setted).

Joseph
  • 401
  • 2
  • 6
  • 10
  • 6
    You need to find the documentation to `strtoul()` and read it before asking the question here. Stack Overflow is a wonderful community, but it's bordering on the impolite for you to not even attempt to read the documentation before asking us to spend our time doing that for you. – David Heffernan Jun 05 '11 at 20:57
  • Hey, close voters: "should have used Google first" is not in any of the descriptions for reasons to close. – Chris Lutz Jun 05 '11 at 21:06
  • read it before posting (I'm not a moron) but I have no idea of what a "base" number is. Kind of math-noob here. – Joseph Jun 05 '11 at 21:07
  • asking "Can you help me to understand the documentation about strtoul" would have been so different? – Joseph Jun 05 '11 at 21:08
  • @Joseph - Yes, it would have. It would have implied that you tried and were having trouble. This question, as worded, implies that you didn't try and don't really understand C. – Chris Lutz Jun 05 '11 at 21:15
  • Well, I've tried short before. You're computation was wrong, then. – Joseph Jun 05 '11 at 21:34

3 Answers3

7

Because you're calling it with the wrong number of arguments. Try

strtoul(argv[1], 0, 0);

Or if you want to enforce base-10 only:

strtoul(argv[1], 0, 10);

Be sure you included <stdlib.h> too!

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • +1 but using 0 for `NULL` kinda rubs me wrong... – Chris Lutz Jun 05 '11 at 21:05
  • 4
    I'll accept that it's personal preference, but I feel like using `NULL` gives one a false sense of safety when calling variadic functions or (uhg) functions lacking prototype. The implementation is allowed to define `NULL` as any null pointer constant, so `0`, `(void *)0`, or even `(1-1)` would be valid. Using `NULL` hides the non-portable assumption that `NULL` is defined as `(void *)0`, which leads to code that will break if that assumption is false. Using `0` catches the few cases (like variadic functions) where you really do need a cast and prompts you to fix them. – R.. GitHub STOP HELPING ICE Jun 05 '11 at 21:09
  • I think it's only allowed to be `0` or `(void *)0` but I'm on my iPhone so I can't check the standard. Wouldn't it be better to use `#define MYNULL ((void *)0)` then? – Chris Lutz Jun 05 '11 at 21:12
  • Note that with variadic functions, it's sometimes actually `(char *)0` you need. My preference is just to use the correct explicit cast when it's necessary, and otherwise use plain 0. But I can see how some people would dislike that, too. (By the way, plain 0 is also more C++-friendly, which some people may care about. I don't write worst-of-both-worlds code though. :-) ) – R.. GitHub STOP HELPING ICE Jun 05 '11 at 21:27
  • 2
    @Chris: 6.3.2.3/3: "An integer constant expression with the value 0, or such an expression cast to type `void *`, is called a *null pointer constant*". 7.17/3: "The macros are `NULL` which expands to an implementation-defined null pointer constant...". `(1-1)` is an ICE with value 0. So is `0L` for that matter. – Steve Jessop Jun 05 '11 at 23:20
3

The other parameters aren't for "how long the number will be". Read the manpage.

unsigned strtoul(char *s, char **endptr, int base);

endptr should be either NULL or the address of a char *. If it's not NULL the function sets the pointer to point to the first unused character in the string, so either a non-digit or the nul-termination.

base specifies what base the number is in. It can be between 2 and 52 (I believe). You probably want 10. As a special case, base 0 checks for a prefix of 0x for hexidecimal and 0 for octal (and none for decimal) and converts accordingly.

Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
1

Try this (for decimal value):

strtoul(argv[1], NULL, 10)
patapizza
  • 2,398
  • 15
  • 14