CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
The use of arrays in C is often a problem for the beginner. The
declaration of arrays isn't too difficult, especially the one-dimensional ones,
but a constant source of confusion is the fact that their indices always count
from 0. To declare an array of 5 int
s,
the declaration would look like this:
In array declarations C uses square brackets, as you can see. There is no
support for arrays with indices whose ranges do not start at 0 and go up; in
the example, the valid array elements are something[
to something[4]
.
Notice very carefully that something[
is not a valid array
element.
This program reads some characters from its input, sorts them into the order suggested by their representation, then writes them back out. Work out what it does for yourself; the algorithm won't be given much attention in the explanation which follows.
#include <stdio>Example 1.4
You might note that the defined constant ARSIZE
is used everywhere instead of the actual array size. Because of that, to change
the maximum number of characters that can be sorted by this program simply
involves a change to one line and then re-compiling. Not so obvious but
critical to the safety of the program is the detection of the array becoming
full. Look carefully; you'll find that the program stops when element ARSIZE-1
has been filled. That is
because in an N
element
array, only elements
through to N-1
are available
(giving N
in total).
Unlike some other languages it is unlikely that you will be told if you 'run off' the end of an array in C. It results in what is known as undefined behaviour on the part of your program, this generally being to produce obscure errors in the future. Most skilled programmers avoid this happening by rigorous testing to make sure either that it can't happen given the particular algorithm in use, or by putting in an explicit test before accessing a particular member of an array. This is a common source of run-time errors in C; you have been warned.
Arrays always number from
;
you have no choice.
A n
-element array has members which number from
to n-1
only. Element n
does not
exist and to access it is a big mistake.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 665
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved