spring break

Mar 12, 2004 21:44

Today's link is thanks to arwenn:
It's not the onion, unfortunately
We should've let the south secede. At least then we wouldn't have to put up with their idiocy screwing up politics for the rest of us. Be sure to take a look at the second place middle school division.

Anyway, lately i've been sending out resumes like crazy all over the place the last couple of days. If I don't hear anything, it'll be pretty disappointing. But on the up side, I did get a call from Amazon, and I'll be having an interview with them next week.

For anyone who think they know their C/C++, I suggest taking a look at "How Not to Program in C++"
I dropped by Barnes and Nobles yesterday, bought a mocha frap, and I got almost all of the ones I tried to figure out - there were a few that were just too much code to look through to be worthwhile, so I just looked at the answer for those.

Here's one of the ones I missed (mostly because I didn't look carefully enough)


#include
#include

int main(int argc, char** argv) {
printf("Hello ");
fork();
printf("World!\n");
return 0;
}
So what does it output, eh smartie pants? (and why!)

Here's a pretty cool piece of trivia regarding bit fields:


#include

//use named bits for clarity
struct access_privledges {
int read:1;
int write:1;
int execute:1;
int violate:1;
};

int main(int argc, char** argv) {
//initialization
struct access_privledges ap;
memset(&ap,0,sizeof(ap));

//let's give ourselves write access
ap.write=1;

//now, let's check to see if we have access
if(ap.write==1)
printf("We can write\n");
else
printf("We don't have write\n");

return 0;
}
So, what do you think is going to happen?
It warms my heart that gcc will give a warning for this problem, even without -Wall. But that would make it too easy. Let's pretend you don't see the warning in amongst the hundreds of other warning you already have in your code, because you're a Real Programmer, right? :)

And finally, here's my favorite bit of C minutia, which I picked up a few months ago, and unfortunately isn't in this book (although it has a lot of other good tricks to watch out for - 113 others actually)

As opposed to the others, this one actually produces the result you would initially expect.
However, look closely at that printf.

#include

int main(int argc, char** argv) {
//initialization
unsigned int i;
double arr[4] = { 149.1, 162.2, 536.3, 4964.4 };

//let's display the array
for(i=0; i<4; i++)
printf("Element %d is %g\n",i,i[arr]);
}
You may notice that I said i[arr] instead of arr[i] as most sane people would use. Sure you could do that too, but the 733t among us would rather use the symmetric ability of the [] operator. That's right, you heard me. [] is symmetric! The two forms are equivalent! That's completely valid! gcc won't even bat an eye, even with -Wall.
When I showed this to Tom, we spent a good 10 minutes playing with it, but couldn't break it, even with nastiness like arrays of pointers. Perhaps it's not so horrible from a correctness standpoint, just from a readability standpoint.
Previous post Next post
Up