An array is a sequence in memory from a start address to an
ending address, consider this example:
int myNum;
myNum is declared as an integer, as soon as the compiler recognizes
that - it reserves an address in memory to hold any value put into myNum.
Now lets say you want to hold 10 individual numbers, you could either
declare 10 variables or you could use an array. To declare an array
we simply tell the compiler how many of this variable we want, then it
will allocate the appropriate amount of memory addresses.
This is what it would look like:
int myNum[ 10 ];
now we have told the compiler to reserve 10 memory spaces for 10 integers.
To place values in them, you simply specifiy which element of the array should hold the value...
myNum[ 0 ] = 344;
myNum[ 1 ] = 5754;
notice that the array is 0 based, meaning it starts at 0. So for an array
declared with 10 elements you would fill 0 through 9
myNum[ 9 ] = 123445;
if you try and fill 10, you are actually filling the 11th memory address.
(0 through 9 add up to 10, so 0 through 10 add up to 11). If you do:
myNum[ 10 ] = 2342;
it will still compile but this is what will happen: When int myNum[ 10 ]
is declared, the compiler sets aside 10 memory address, we'll say it is
addresses 0x00aabb00 through 0x00aabb0a. When you write to myNum[ 10 ] you
are actually writing to one memory address higher than what the compiler set
aside, in this example you would be writing to 0x00aabb0b. What you would be
doing is writing to some value in memory that the computer might be using for
something else. Your program could crash or you could get unexpected results!
You can use variables for which element of the array you want to access.
Example:
int a = 2;
scanf( "%d", &myNum[ a ] );
this would accept user input into the third address set aside for myNum (myNum[ 2 ]).
Once again, remember it's 0 based - so it is like this:
myNum[ 0 ] is the first slot
myNum[ 1 ] is the second slot
myNum[ 2 ] is the third slot...
Be careful not to do this:
scanf( "%d", myNum );
notice you didn't use the "address of" operator (ampersand) and you didn't use
an array element. This will compile, but it will always put the result into the
first element, myNum[ 0 ]. This is because myNum is declared as an array so the
first address it points to is myNum[ 0 ] which is what scanf will use.
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
void main( void )
{
int array[ 7 ];
int times;
printf("\n Enter 5 numbers separated by spaces: ");
for ( times = 0; times < 5; times++ )
{
scanf( "%d", &array[ times ] );
}
array[5] = array[ 0 ] + array[ 1 ] +
array[ 2 ] + array[ 3 ] + array[ 4 ];
array[6] = array[ 5 ] / 5;
printf( "\n The sum is: %d\n", array[ 6 ] );
getch( );
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
void main( void )
{
int array[ 5 ];
int sum;
int div;
printf( "\n Enter a number:" );
scanf( "%d", &array[ 0 ] );
printf( " Enter a number:" );
scanf( "%d", &array[ 1 ] );
printf( " Enter a number:" );
scanf( "%d", &array[ 2 ] );
printf( " Enter a number:" );
scanf( "%d", &array[ 3 ] );
printf( " Enter a number:" );
scanf( "%d", &array[ 4 ] );
sum = array[ 0 ] + array[ 1 ] + array[ 2 ] +
array[ 3 ] + array[ 4 ];
div = sum / 5;
printf( "\n Their sum is: %d\n", div );
getch( );
}