Why is this program, which is performing invalid pointer initialization, compiling to C?

I wrote a simple C program and I expected it to fail in compilation, but unfortunately it compiles and works fine in C but fails compilation in C ++. Consider the program below:

#include <stdio.h>
int main()
{
    char *c=333;
    int *i=333;
    long *l=333;
    float *f=333;
    double *d=333;
    printf("c = %u, c+1 = %u",c,c+1);
    return 0;
}

      

Follow this link: http://ideone.com/vnKZnx

I think this program definitely cannot compile in C ++ due to strong C ++ type checking. Why does this program compile to C? It is the fact that the compiler also shows warnings. I am using Orwell Dev C ++ IDE (gcc compiler 4.8.1). I also tried the same program on another compiler (Borland Turbo C ++ 4.5), saved it by extension .c and in that compiler it was unable to compile.

+3


source to share


3 answers


This code is neither legal nor legal C ++.

N1570 Β§6.7.9 / p11:

The initializer for a scalar must be a single expression, optionally enclosed in curly braces. The initial value of the object is the value of the expression (after conversion); the same type restrictions and conversions for simple assignment apply, taking the form of a scalar to be an unqualified version of its declared type.

Β§6.5.16.1 / p1 stipulates that for a simple assignment:



One of the following:

  • the left operand is of atomic, qualified, or unqualified arithmetic type, and the right operand is of arithmetic type;
  • the left operand has an atomic, qualified, or unqualified version of a structure or union type that is compatible with the right type;
  • the left operand is of atomic, qualified, or unqualified pointer type and (given the type that the left operand would have after an lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left is all type qualifiers to which points to the right;
  • the left operand is of an atomic, qualified or unqualified pointer type, and (given the type that the left operand would have after an lvalue conversion) one operand is a pointer to the type of the object and the other is a pointer to the qualified or unqualified version void

    , and the type that the left ones point to, has all qualifiers of the type specified by law;
  • the left operand is an atomic, qualified, or unqualified pointer, and the right is a null pointer constant; or
  • the left operand is atomic, qualified, or unqualified _Bool

    , and the right operand is a pointer.

None of them match the left and 333

right pointer . Β§6.5.16.1 / p1 is a constraint and corresponding implementations are required to perform diagnostics on constraint violation (Β§5.1.1.3 / p1):

A conforming implementation MUST give at least one diagnostic message (defined by implementation) if a preprocessing translation unit or translation unit violates any syntax rule or constraint, even if the behavior is also explicitly declared undefined or implementation-defined.

It happens that GCC decides to generate a warning instead of an error in C mode and continue compiling, but this is not required.

+7


source


C can convert numbers to pointers. char* c = 123

will set c

to point to the 123rd byte in memory.



While this is almost useless and almost certainly a desktop programming error, embedded systems need to interact with hardware that can look for values ​​in specific, hard-coded memory addresses.

+5


source


There is nothing ethnically wrong with the code. You initialize the pointers with integer constant values ​​333 and then you print the address. The warning it shows is probably because the integer value is of the type assigned to the address type.

the problem starts when you try to dereference pointers. This will give a segmentation fault.

-2


source







All Articles