Email or username:

Password:

Forgot your password?
5 comments
DrYak

@aburka it will segfault and crash.
The key is type errors: in C kanguage "a" is a string, but 'a' has a completely different meaning: it's a character. Basically a number in the ASCII table (I am over simplyfing).
So when GCC sees 'Hello world', it interprets it as 'H' (and complains about the excedent letters - they are not proper UTF-8 multibyte) and interprets it as 72. Then C will automatically convert the number 72 into a pointer (hence the other warning).
...

DrYak

@aburka ...Running it, printf will attempt to find a format string at location 72 of the memory. And crash.

There are helpers in GCC for printf, but currently they only check if *the static string* is a proper format and match the argument. They currently cannot check that the wrong type (a character instead of a string) was provided.
C++ is more peeky about types and would probably complain about not converting int to pointer, but still no explanation why 'H' is considered int.

DrYak

@dryak that would require an intimate knowledge of the C language and its typing system (not trivial depending on one's background).
It's mostly a leak abstraction if the underneath machine code.

And a complete overhaul of how C is parsed in order to carry over the necessary extra information about type to detect this.

BUT!...

DrYak

@dryak ...BUT a proper linter should immediately spot the problem:

There are way too many stuff between the single quotes in the source code for a proper character.
Are you sure you really want a char/an int8/an integet? Didn't you mean a string instead and forgot the double quotes?

Peter Ludemann

@dryak @aburka Programmers don't like to specify conversions manually, so that's what you get, although C makes it extra spicy by quietly ignoring loss of precision or sign (and compilers don't catch all such errors, it seems).
Speaking of conversions, I got some amazing error messages from some C++ code that had user-defined conversion operators for integers, pointers, and subscripting.

Go Up