On Jan 19, 2008, at 6:59 PM, Michael G Schwern wrote: > Here is a perfect example of a total mindset mismatch I experience > when reading a lot of C tutorials and examples. There's only one > word in my mind when I read the above... "So?" > > It's to do with the way it's presented. There's so much unspoken > and assumed knowledge in there beyond simple syntax issues. However, there was enough information there for a motivated individual to follow up. I provided a link to the germane Wikipedia article; at the bottom of that article are links to several others. The best one is the first listed, from IBM Developer Works. http://www-128.ibm.com/developerworks/library/pa-dalign/ I didn't think it was necessary to go into that level of detail in a mailing-list post; not when it was possible to springboard off the Wikipedia article. Kudos to Andy Armstrong for taking the time to provide a more thorough answer. > What the hell is *(int*) and why is it there? That's pointer dereferencing syntax and a cast. As mentioned earlier, references in Perl... my $foo = 1; my $foo_ref = \$foo; my $bar = $$foo_ref; ... are analogous to pointers in C: int foo = 1; int *ptr_to_foo = &foo; int bar = *ptr_to_foo; /* bar is now 1 */ The cast means, more or less, "convert this to something else". C's conversion rules can be tricky, but casting between a char* and an int* is fairly straightforward (provided that the pointer variables are aligned). Assume for simplicity's sake that a char is 8 bits and an int is 32 bits. char *ptr_to_char = malloc(sizeof(int)); ptr_to_char now holds a memory address. At that memory address are 4 bytes of uninitialized memory supplied by malloc(). memcpy(ptr_to_char, &foo, sizeof(int)); After the memcpy operation, the 4 bytes pointed to by ptr_to_char now hold the same values as the 4 bytes of memory that hold the int foo. So, if we pretend that ptr_to_char is really a pointer-to-int, we can use those bytes to represent an int. int *ptr_to_int = (int*)ptr_to_char; int baz = *ptr_to_int; /* baz is 1 */ The *(int*) idiom just combines the cast and the dereference, so that you don't need the intermediate variable. > Shouldn't the language do this for me? Not if it's a low-level language. (Though, ironically, C was considered a high-level language when it was introduced.) Pointers and dereferencing are basic stuff. IMO, it's time for you to just suck it up and go write some C code. Your dogmatic laziness is admirable, but it's getting in your way. You're a bright guy; picking up the fundamentals of C isn't going to take you long. Cheers, Marvin Humphrey Rectangular Research http://www.rectangular.com/Thread Previous | Thread Next