LECTURE 15: Arrays as Function Arguments / Advanced Topics about Arrays

 

Using Array Elements as Function Arguments (Section 8.4)

Let us assume an array tictactoe is declared as

char tictactoe[3][3];

Array elements, such as tictactoe[0][2] can be used as function arguments in the same way as single-valued variables.

 

Example:

scanf(“%d”,&tictactoe[0][2]);

 

 

Array Arguments (Section 8.5)

 

Arrays can also be used as function arguments! Their use is relatively simple. Let us look at the following example.

 

Using one-dimensional arrays as arguments: Fig 8.15

 

# include<stdio.h>

# define NOT_FOUND -1   /* Value returned by search function if target not found */

 

int main(void)

{

       int scores[3] = {100, 60, 80};

int pos;

 

       pos = search(scores, 60, 3);     

}

 

/*

 *  Searches for target item in first n elements of array arr

 *  Returns index of target or NOT_FOUND

 *  Pre:  target and first n elements of array arr are defined and n>=0

 */

int

search(const int arr[],  /* input - array to search    */

       int       target, /* input - value searched for */

       int       n)      /* input - number of elements to search     */

{

      int i,

          found = 0,     /*  whether or not target has been found    */

          where;         /*  index where target found or NOT_FOUND   */

 

      /*  Compares each element to target       */

      i = 0;

      while (!found && i < n) {

          if (arr[i] == target)

                found = 1;

          else

                i++;

      }

 

      /* Returns index of element matching target or NOT_FOUND       */

      if (found)

            where = i;

      else

            where = NOT_FOUND;

 

      return (where);

}

 

Explanation of the program:

-          in the main function, the goal is to find if any element of the first 3 elements of scores array has value equal to 60. If the answer is affirmative, the function returns the position of the first element equal to the search value

-          search function has 3 arguments: the first is an integer array arr, the second is search variable target, and the third is variable n that tells how many elements of array arr are considered

-          const in expression “const int arr[]” means that values of arr array cannot be changed within the function search

 

 

Using two-dimensional arrays as arguments: Fig 8.19

 

 

/*   Checks whether a tic-tac-toe board is completely filled.  */

int

filled(char ttt_brd[ ][3])  /*  input - tic-tac-toe board     */

{

      int r, c,  /* row and column subscripts   */

         ans;   /* whether or not board filled */

 

      /*  Assumes board is filled until blank is found */

      ans = 1;

 

      /*  Resets ans to zero if a blank is found       */

      for  (r = 0;  r < 3;  r++)

         for  (c = 0;  c < 3;  c++)

             if (ttt_brd[r][c] == ' ')

                  ans = 0;

 

      return (ans);

}

 

Explanation of the function filled:

-          function filled has only one argument, array ttt_brd

-          expression “char ttt_brd[][3]” tells us that

o        ttt_brd is two-dimensional array

o        ttt_brd is of of char type

o        ttt_brd has three columns. It is very important that content of first bracket is empty and the content of the second bracket is the number of columns!!

 

 

Advanced topic: Arrays as Pointers

 

Declaration

int scores[3];

reserves 3 consecutive spaces in memory to store 3 integers

 

Surprisingly, if you printf the value of scores as

printf(“%d \n”,scores);

you will see the address of the first element of array scores!!

 

-          Therefore, scores is a pointer to the first element of array scores[3]!!

-          Following this reasoning, scores+1 is a pointer to the second element of array scores[3].

-          Therefore *scores equals scores[0], *(scores+1) equals scores[1], etc…

 

Important consequence, and source of programming errors:

 

If you declare

int scores[3];

int result;

and in your program you write

result = scores[2] + scores[3];

the value of result will become some junk value!!