4 ms·
Not exactly a compiler bug, but Borland Turbo C (which predates ANSI C) had bizarre semantics around character pointers such that char *s was almost a managed s
by torstenvl 3y ago
Not exactly a compiler bug, but Borland Turbo C (which predates ANSI C) had bizarre semantics around character pointers such that char *s was almost a managed string type, and assignment would make a deep copy of the string.... much to the surprise of anyone expecting identical addresses.
The manual maybe (?) hints at this behavior:
> Using a Character Pointer
> The second method you can use to define strings is a character pointer. Edit your program to look like this:
main ()
{
char *msg;
msg = "Hello, world";
puts(msg);
}
> The asterisk (*) in front of msg tells the compiler that msg is a pointer to a character; in other words, msg can hold the address of some character. However, the compiler sets aside no space to store characters and does not initialize msg to any particular value.
> When the compiler finds the statement
msg = "Hello, world\n";
it does two things:
- As before, it creates the string "Hello, world\n", followed by a null character somewhere within the object code file.
- It assigns to the starting address of that string-the address of the character H-to msg.
> The command puts(msg) works just as it did before, printing characters until it encounters the null character.
The workaround is of course to use s1 = &s2[0]