7 ms·
This works in Standard C: int *p = 3;
by WalterBright 4y ago
This works in Standard C:
int *p = 3;
- kevin_thibedeau 4y agoThe address is still 3 which has valid applications. C is permissive enough to run on platforms that don't use address 0 for NULL. With pointer operations the compiler will change the encoding from 0 to that platform's NULL address. int *p = 0; intptr_t i = (intptr_t)p; if(i == 0) ... // Isn't always true
- LegionMammal978 4y agoThat's a compiler extension. In C17, 6.5.16.1 (Simple assignment) implies that the RHS of an assignment to a pointer must either have pointer type or be a null pointer constant (i.e., an integer constant equal to 0, or such a constant casted to pointer type), and 6.7.9 (Initialization) states that "the same type constraints and conversions as for simple assignment apply" to expressions used as initializers.
- Kamq 4y ago"standard" C implies default to me, so the post is probably referring to C89 or C99, which are significantly more used than C17.
- LegionMammal978 4y agoThis particular rule is essentially unchanged from C89 (3.5.7 for initialization, 3.3.16.1 for simple assignment) and from C99 (6.7.8 for initialization, 6.5.16.1 for simple assignment). Generally, most existing rules don't change much at all across the C Standard versions.
- ynik 4y ago"Standard C" is the C that was standardized. This is not what you get from your compiler by default! With gcc, you have to use `gcc -std=c99 -pedantic-errors`, otherwise it will accept programs which are invalid according to the standard.
- Gibbon1 4y agoI like to point out that AVR micro's reading and writing to address 0 is legit. On the ARM Cortex I think address 0 is the initial stack pointer value. My opinion is NULL being something special in the language is mathy CS academics trying to turn C into a mathy abstract language. Which it ain't.
- jcelerier 4y agoC is officially a mathy abstract language since 1989, the compilers just took some time to catch up. > 2.1.2.3 Program execution > The semantic descriptions in this Standard describe the behavior of an abstract machine [...]
- Gibbon1 4y agoThe mathy version is C is useless for real programs.