On Dec-01, Dan Sugalski wrote: > > C, for example, is weakly typed. That is, while you tell the system > that a variable is one thing or another (an int, or a float), you're > perfectly welcome to treat it as another type. This is *especially* > true of values you get to via pointers. For example, this snippet > (and yes, it's a bit more explicit than it needs to be. Cope, you > pedants :): > > char foo[4] = "abcd"; > printf("%i", *(int *)&foo[0]); > > tells the C compiler that foo is a 4 character string with the value > "abcd", but in the next statement we get a pointer to the start of > the string, tell the compiler "No, really, this is a pointer to an > int. Really!" and then dereference it as if the string "abcd" really > *was* an integer. If C were strongly typed you couldn't do that.
A wholly off-topic comment about how to make use of this: When running gdb on a C (or better, STL-happy C++) program, it's nice to be able to set conditional breakpoints. b myfile.c:328 cond 1 somevar == 17 but gdb can often get confused or very slow if you try to do something similar with a char* value: cond 1 strcmp(mystring,"badness") == 0 It goes crazy making a function call every time that breakpoint is reached. I can get gdb to segfault this way without too much trouble. So instead, use this trick: cond 1 *(int*)mystring == *(int*)"badness" and it'll go back to doing a simple integer comparison. And it's very fast about that. Note that because you're using a probably 32-bit integer, that isn't really looking at the whole string; it'll have exactly the same effect if you say cond 1 *(int*)mystring == *(int*)"badn" I often use this in combination with std::basic_string types in C++, since the templatized types and other implementation-dependent weirdnesses end up making things much harder than if you were using simple char*'s. So it would look something like: cond 1 *(int*)mystring.data() == *(int*)"badn" Or maybe it's slightly safer to do this, I dunno: cond 1 *(int*)mystring.c_str() == *(int*)"badn" Sorry for the diversion. Um... if I had to say something on-topic, I'd point out that Perl's type system isn't complete ("not THAT strong"), since there some corners in the language where you can sneak around it. pack("p"), some system calls, and other things I can't think of. But maybe the very oddness of those things is evidence that Perl does indeed have a strong type system. ("Strong" in the technical, not comparative, sense.) Not that anyone seems to be able to agree on the exact definition of "strong typing".