CFORBEGINNERS.COM

Search Google

C Arrays

Arrays are convenient tools at your disposal when you want to store multiple values of a certain data type. When arrays are used inside of loops they become even more useful with storing certain data. Think of a variable as a small box, now think of an array as a single big box that can hold a whole bunch of little boxes (variables). Instead of having to search for little boxes you can put them all in one bigger box, label it, and have it at your quick disposal. For instance, up until now if you wanted to store multiple values for integer types you would probably do so like this.

int x=10;
int y=23;
int z=38;

It would be much more convenient if we could store these different values into one variable and call upon them as we please. Fortunately, this is possible through arrays, here is how you declare one.

int my_array[3];

This is how you declare an array. As always, int means that this array will store integer data types, my_array is simply the name of this array and the [3] means that it will hold three values. Once you have declared your array you're going to want to assign each integer a value, this is how you do so.

my_array={10,23,38};

Remember that this array holds three values, 10,23,and 38 are the values it will store, put these values in curly braces not the brackets,the brackets are used when you declare how many values are in the array in this case 3. You could also initialize an array individually like this.

my_array[0]=10;
my_array[1]=23;
my_array[2]=38;

I find the first way to more convenient however this way may be more clear, I will explain why the index number starts at [0] in a moment. Now that we have these integers stored in my_array lets print them out.

printf("%i",my_array[0] );

This is where it can get a little confusing, but just in the beginning so bare with me. Where did the [0] come from? The [0] will print out 10, this is because 10 is the first value in my_array. If we were to use my_array[1] it would've printed out to the screen 23 and if we used my_array[2] it would've printed out to the screen 38. Yes, it would make more sense for [1] to be 10, [2] to be 23, and [3] to be 38 but easy isn't always the most efficient. In c programming arrays are used from 0 to N-1, for instance with an array of 5 values its max index number would be [4]. Some languages do it the way that makes more sense but once the code is being compiled the array has to be changed into the way that the c language initially uses them. This is because at the machine level there is just a bunch of zeros and ones. Having an array begin at [0] saves processing time and is therefore more efficient compared to if it began at [1]. Now lets take a look at a full working program that uses arrays.

#inlcude <stdio.h>
#include <stdlib.h>

int main()
{
 int k;
 int my_array[5]; /*declares an array that will hold 5 integers*/
 my_array={10,20,30,40,50}; /*these are the values of the 5 integers*/
for(k=0; k<5; k++)
  {
    printf("%i\n",my_array[k] ); /*when k is 0 my_array will print out 10 and so on all the way to k being 4*/
   }
system ("pause");
return 0;
}

This program uses a for loop that executes 5 times each loop k is incremented by 1. In the second loop k is now at 1, therefore 1 is put into my_array[k]. my_array[1] will print out 20, after the loop executes k will be incremented by one making it 2, 2 will go into the place of k, my_array[2] will print out 30 and so on. If you are still a little confused it helps to code it and see how the program works yourself, doing this helps you get a better grasp on arrays and programming in general.

Arrays in Functions

Arrays can be manipulated through functions, unlike variable arrays work differently in functions. When a variable is used in a functions a copy of that variable is made and sent to the function, this saves the user from accidentally ever changing a variable within a function and losing those values permanently. Arrays work differently, instead of a copy being made the actual address is sent to the function (it would take a lot of memory to send copies), this being said, be sure to always be mindful of data in arrays when using functions, precious information could be lost. Aside from a few alterations, programming with arrays in functions isn't too different. Here is how a prototype of a function called array with one array parameter.

int array(int x [ ] );

This function will take in an array with integers and return a single integer, this would be ideal if we wanted the function to take all the values in the x array, add them up and return a single sum. If instead we wanted the whole array returned we would use void instead of int like this.

void array(int x[ ] );

void is used whenever we want more than a single value returned from a function, below is perfect example of a program that takes in ten values using an array, adds two to each of those values and returns the altered array back to the main program.


#include <stdio.h>
#include <stdlib.h>

void array_function(int x[ ] );
int main()
{

    int k;
    int y[10]; /*declares an array named y that will 10 integer values
    for(k=0; k<=9; k++)
    
        scanf("%i",&y[k]); /*scans in a value 10 times*/
    }
    array_function(y); /*calls the functions and sends it the values stored in array y */

    for(k=0; k<=9; k++)
    {
        printf("\n%i",y[k]);/*after the function call, this loop prints out the new values*/
    }
    return 0;
}

void array_function(int x[]) /*defining the function */
{
    int i;
    for(i=0; i<=9; i++) /*a loop that executes 10 times*/
    {
x[i]=x[i]+2; /*x[i] equals what it was plus 2*/ } }