3 ms·
Hmm. Probably true. What about c == p? I don't recall whether both will promote to numeric, or whether it's a type error. For that matter, what does (uncast
by AnimalMuppet 7y ago
Hmm. Probably true. What about c == p? I don't recall whether both will promote to numeric, or whether it's a type error.
For that matter, what does (uncasted) p = c do? How about c = p?
[Edit: You updated while I was writing; my first question you already answered. Warning, not error.]
- thaumasiotes 7y ago$ cat pointers.c #include "stdio.h" int main(int argc, char* argv[]) { char Z = 'Z'; char q = 'q'; void* null = 0; printf("Z is \\x%02x\n", Z); printf("But if it were a pointer, it would be %08x\n", Z); printf("Watch this:\n\n"); null = Z; /* %p to print a pointer value */ printf("Our void* is now: %p\n", null); q = null; printf("And q is: %c\n", q); return 0; } $ gcc -o pointers pointers.c pointers.c: In function ‘main’: pointers.c:12:7: warning: assignment makes pointer from integer without a cast [-Wint-conversion] null = Z; ^ pointers.c:17:4: warning: assignment makes integer from pointer without a cast [-Wint-conversion] q = null; ^ $ ./pointers Z is \x5a But if it were a pointer, it would be 0000005a Watch this: Our void* is now: 0x5a And q is: Z $ The assignments are warnings. They work just like you'd expect them to work. Notice all the different printf flags? This is why you need them.
- spc476 7y agoThe literal 0 in a pointer context will be converted to a NULL pointer, which can be a non-zero bit pattern (there are some systems where the actual NULL pointer isn't all zeros). Going through a variable might not do what you think. So this: char *p = 0; is fine, but intptr_t a = 0; char *p = (char *)a; might not do what you expect (set p to the NULL pointer).