This time we converted "Hello World" in a modular program. One module contains the message, and one sends the greeting.

Well, that's the way it's supposed to work, something funny is happening.

hello_sub.c

// Message to say hello
const char[] message = "Hello World!\n";	

hello_main.c

#include <stdio.h>
extern const char* const message;
int main()
{
    printf(message);
    return(0);
}

Source code for the string
Source code for main

Hint 1: C is not typesafe.

Hint 2: The types char* and char[] are almost interchangeable.

Hint 3: "Almost" is a very significant word.

Hint 4: What's located at memory location 0x48656C6C?

Hint 5: The value of "H" is 0x48. The value of "Hell" is 0x48656C6C. 0x486656C6C is not a good pointer.

Hint 6: What does "Hello World!\n" and 0x48656C6C have in common? The same thing that char* and char[] do.

Answer: C++ is not completely typesafe. This is one of those cases where things break down. In the module the string is defined as a character array:

const char[] message = "Hello World!\n";	

In the main it's referenced as a character pointer. That means that C (in main) thinks that the variable holds a 4 byte pointer, not a bunch of characters. So the program grabs the first 4 bytes of the array, treats them as a pointer and tries to print. The result is a mess.

Main gallery