What is end after strtol("0x", &end, 0)? 
Author Message
 What is end after strtol("0x", &end, 0)?

I think strtol("0x", &end, 0) should take "0" as the subject sequence
and "x" as the final string; thus end should point to 'x' after the call.
This is because "0" is the longest initial subsequence of "0x" that is
of the expected form.

However, with Microsoft Visual C++ 6.0 on NT4 SP6a, end actually
points to the '0', as if the subject sequence were empty or didn't have
the expected form.

Is this a bug in the C library?
If so, has it been fixed in a later version?

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  static const char s[] = "0x";
  char *end;
  long ret;

  printf("converting string \"%s\"\n", s);
  errno = 0;
  ret = strtol(s, &end, 0);
  printf("errno is %d\n", (int) errno);
  printf("strtol returned %ld\n", ret);
  printf("final string begins at offset %d: \"%s\"\n", (int) (end - s),
end);
  return 0;

Quote:
}



Sat, 26 Mar 2005 21:21:50 GMT  
 What is end after strtol("0x", &end, 0)?

Quote:
> I think strtol("0x", &end, 0) should take "0" as the subject sequence
> and "x" as the final string; thus end should point to 'x' after the call.
> This is because "0" is the longest initial subsequence of "0x" that is
> of the expected form.

Using 0 as the third parameter to strtol causes numbers to be parsed as integer constants using C/C++ syntax. If the string starts with 0x the string is interpreted as a hexadecimal literal. But 0x is no valid hex number. So, IMHO, end may point to the location containing '0' or to the character following 'x', but not to the byte containing the 'x'.

Regards
    Heinz



Sat, 26 Mar 2005 21:34:13 GMT  
 What is end after strtol("0x", &end, 0)?

Quote:
> Using 0 as the third parameter to strtol causes numbers to be parsed as
integer
> constants using C/C++ syntax.

Except strtol allows a sign and does not allow an integer suffix.

Quote:
> If the string starts with 0x the string is interpreted as a hexadecimal

literal.

If the *subject sequence* starts with 0x then the *subject sequence* is
interpreted
as a hexadecimal literal.  "The subject sequence is defined as the longest
initial
subsequence of the input string, starting with the first non-white-space
character,
that is of the expected form."  The subject sequence cannot be "0x", because
that
is not of the expected form; but it can be "0".



Sun, 27 Mar 2005 00:37:02 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Catching "End paint" message

2. Get "End Task" dialog on Logoff

3. help with "strtol"

4. "#include " generates "C1010" Unexpected End of File

5. how do i place an end of string char at the end of a string

6. Loosing Memory when Start end ends an Dialog

7. ending a blocing sockets call (am using MFC's CSocket class)

8. Why you should start using "%X" and "0x".

9. I am going to study "C"

10. remove() vrs fopen("""w")

11. Displaying binary data as ascii "1"'s and "0"'s

12. Looking for "Shroud"/"Obfus"

 

 
Powered by phpBB® Forum Software