Created
December 11, 2010 04:42
-
-
Save doolin/737150 to your computer and use it in GitHub Desktop.
Casting 0 to \0 (or 0x0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Here's a stupid c trick, a variant of | |
| which can come up when doing | |
| `char` and `char *` operations. For an example, | |
| an older version of make (ca. 1994) traversed | |
| a char[] and returned the pointer position of | |
| a `#` to indicate the start of a comment. This | |
| produces a warning in gcc now. | |
| Here's the code: | |
| */ | |
| #include <stdio.h> | |
| int | |
| main() { | |
| printf("(char *)0: %p, %d\n", (char *)0, (char *)0); | |
| return 0; | |
| } | |
| /* | |
| Compile this code with `gcc -Wall -pedantic char0.c` | |
| I get this for compiler output in gcc 4.4: | |
| char0.c: In function ‘main’: | |
| char0.c:4: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char *’ | |
| Run it: | |
| $ ./a.out | |
| (char *)0: 0x0, 0 | |
| $ | |
| Why is this important? If you have to test for pointer position on | |
| a desired character, and that character doesn't exist, make sure | |
| the test is against something appropriate. `cast` is your friend here. | |
| If this bothers anyone, take it up with Roland. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment