Article View: comp.sys.apple2.gno
Article #263Re: Crash............
From: Soenke Behrens
Date: Mon, 16 Nov 1992 11:09
Date: Mon, 16 Nov 1992 11:09
71 lines
3269 bytes
3269 bytes
chauhan@aristotle.ils.nwu.edu (Amrit Chauhan) writes:
>I am in the process of learning C, and obviously, I have some really dinky C
>programs. Well, this one crashes...in very weird ways. Take a look:
Ok, let's have a look.
>/* prob8.c -- Programming Exercises (Chapter VI, Problem 8) */
>#include <stdio.h>
>#define MAX 7
>main()
>{
Don't do that. It's not illegal, but better be explicit. I.e.
int
main (int argc, char *argv[])
{
even if you do not use argc and argv.
> int i, integers[MAX];
> printf("Please enter 8 integer values below:\n");
Aha. Integers is an array with 7 elements, not 8. Note: with
int array[n] you get an array of n elements, which can be accessed using
array[0] through array[n-1].
So, what you are doing is trash memory.
> for(i = 0; i <= MAX; i++)
> scanf("%d", &integers[i]);
Which would be wrong, anyway. When dealing with arrays in a loop, use
asymmetric bounds. Example:
for (i = 0; i < n; i++)
where n is the "size" of our array. The lower bound is exactly "on mark",
the upper bound is one above the end of the array. The good thing about this
is that it's intuitive (as you can use n as bound as opposed to n-1). There
are more advantages, I won't go into them.
I strongly recommend the following book to you: C Traps and Pitfalls, by Andrew
Koenig, ... umm ... I gave it to a friend ... I think Addison Wesley.
Also good books: C - A Reference Manual (better than K&R 2 IMHO), and "Writing
Efficient Programs" by Jon Louis Bentley, Prentice Hall.
Plus, of course, all the others, but that get's expensive :) (All the others
being: Algorithms in C, The Art of Programming by Knuth, Programming Pearls,
etc. etc.)
>}
And here you should return (EXIT_SUCCESS) (after including <stddef.h> of
course), as you have defined main as returning an int (by not specifying
anything). That's a good idea, anyway, otherwise the shell gets a random
result back.
>shell....does anyone have any idea what I'm doing wrong? Keep in mind that
>I'm learning C (put your hands together) -- so, now I'm stuck...it's more than
>likely something I'm doing, and since I can't check my code to an "answer" I
>have to rely on the expertise of those here :)
No problem. Let me repeat: Get that C Traps and Pitfalls. If I had had that
when learning C, I'd had saved myself a lot of trouble.
Soenke
--
===========================================================================
More than silver, more than gold, I need ! behrenss@informatik.
electricity. I need it for my dreams. ! tu-muenchen.de
(RACTER) ! FIDO: 2:246/13 Soenke Behrens
Technical Details
Message-ID:
<behrenss.721912181@hphalle6>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!rpi!usc!sol.ctr.columbia.edu!ira.uka.de!news.belwue.de!math.fu-berlin.de!informatik.tu-muenchen.de!hphalle6!behrenssReferences:
<1992Nov16.073159.14009@ils.nwu.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.apple2.gno Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!rpi!usc!sol.ctr.columbia.edu!ira.uka.de!news.belwue.de!math.fu-berlin.de!informatik.tu-muenchen.de!hphalle6!behrenss From: behrenss@Informatik.TU-Muenchen.DE (Soenke Behrens) Subject: Re: Crash............ References: <1992Nov16.073159.14009@ils.nwu.edu> Sender: news@Informatik.TU-Muenchen.DE (USENET Newssystem) Organization: Technische Universitaet Muenchen, Germany Date: Mon, 16 Nov 1992 11:09:41 GMT Message-ID: <behrenss.721912181@hphalle6> Lines: 71