On a recent trip to my local public library there is a shelf for free books, and to my amazement, there was a copy of Problem Solving & Program Design in C. Sarcasm aside, I couldn't understand why it was still there for my taking. In the chapter on arrays, there is a simple program on writing a stack, I added a print function to this program to list all the elements.
This code compiles, but when I ran it, I only saw the last element in the array. Hmmm...strange. I fooled around a little adding some extra printlines, check some other code snippets and I can't find what is going on!
Eventually, after a day or so, what was wrong was I had a semi-colon at the end of my for loop declaration. The C compiler accepted this code as valid code and when my program ran, the for loop looped over itself, never proceeding into my brackets until i <= *size was actually true, then only printed the 3rd element in my array!
To me, there are several thoughts I took away from this little problem.
- What a great lesson to enforce really reading your code before you execute.
- An increased appreciation for the C# compiler to not let me even make that mistake in C#. (The compiler gives you a warning.)
1If your looking to get a nice start on early American History, I suggest the following:
- John Adams by David McCullough
- 1776 by David McCullough
- Benjamn Franklin by Walter Isaacson
- Founding Brothers by Joseph Ellis
3 comments:
I think that writing on one single line your function is very clumsy and can lead to errors.
I recommand
for(){
}
see id software coding conventions
http://www.geeks3d.com/20081111/id-software-c-coding-conventions/
Nate,
I would like to discuss an Agile/XP software opp with a great client of mine in NYC. Ask Corey Haines what he thought of me - I met him a few wks ago at Jon Kern's place.
Cheers,
Ben Ross
Forrest Solutions
212-204-1025
Thanks anonymous, going back and looking at this post, I see the formatting got really screwed up. I re-published with the code better formatted.
Thanks for the link!
Post a Comment