Back to DFS's C Page


ONE-DIMENSIONAL ARRAYS

One-dimensional arrays are a simple matter once you have worked with strings, which in actuality are one-dimensional char arrays.

Three different methods are illustrated for accessing an array whose address has been passed to a function:

For each of the three techniques, a table is provided showing the step-by-step changes in the values of the arguments and the local variables in the function. The red items in the table indicate what has just changed. The blue numbers for the steps in the table correspond to the blue numbers in comment delimiters in the code for the function. Thus, you can trace through the code, seeing what happens at each step.

A complete sample program is provided at the end of this file.

Array Declaration

Consider an int array as declared below.

int scores[10];

This array can hold up to 10 integer values. As usual in C, subscripts start with zero, so the subscripts range from 0 to 9. To be useful, however, unless the array is always full, we must either use one of the elements as an end-of-list marker or keep a count in another variable. Here we will look at the latter technique using the following variable.

int num_scores;

Calling the Function

Since the name of any array is a pointer by definition, we can call a function which returns the highest score in the array as follows.

high_score = get_highest(scores, num_scores);

Since, by definition, the name of an array is a pointer, the arguments given with result in the address of the array scores and the value of num_scores being passed to the function get_highest(). All three versions of the function use the same basic algorithm:

  1. Assign the value of the zeroth element to the variable highest.
  2. Scan the rest of the array, replacing the value of highest when a higher one is found.
  3. Return the value of highest.

Memory Usage

The following declarations result in forty-eight (48) bytes being allotted, forty for the array and four for each of the two ints.

   int scores[10] = {55, 74, 45, 88, 67};
   int num_scores = 5;
   int high_score;

The following table illustrates how the compiler might handle the variables. Six values are assigned at the time of declaration. The others are represented by ??. For the purposes of demonstrating how the addresses and values are utilized in the three functions below, it has been decided to start the storage of the data at the address 80FADE00.

VariablesValuesSubscriptsAddresses
high_score?? 80FADE00
num_scores5 80FADE04
scores55080FADE08
74180FADE0C
45280FADE10
88380FADE14
67480FADE18
??580FADE1C
??680FADE20
??780FADE24
??880FADE28
??980FADE2C

Back to Top

Subscript Notation

One of the ways of writing get_highest() uses subscript notation. i is used as the subscript and is kept within the usable part of the array.

int get_highest(int s[], int n)  /* 1 */
/* Assumes that there is at least one element */
{
   int i;
   int highest = s[0];           /* 2 */

   for( i = 1; i < n; i++)       /* 3, 5, 6, 8 */
     if ( s[i] > highest)
        highest = s[i];          /* 4, 7 */
   return highest;               /* 9 */
}
Stepsnis[i]highestWhat Happens
180FADE085??????Arguments Passed
280FADE085????55highest Initialized
380FADE08517455i Initialized
480FADE08517474s[1] > highest; highest Replaced
580FADE08524574i Incremented
680FADE08538874i Incremented
780FADE08538888s[3] > highest; highest Replaced
880FADE08546788i Incremented
980FADE08546788Value of highest Returned

Back to Top

Pointer Notation

get_highest() could also be written using pointer notation. i is used simply as a counter to keep the pointer within the usable part of the array. The value of the pointer changes so that each element is pointed at in succession.

int get_highest(int *s, int n)   /* 1 */
/* Assumes that there is at least one element */
{
   int i;
   int highest = *s++;           /* 2, 3 */

   for( i = 1; i < n; i++, s++)  /* 4, 6, 7, 8, 9, 11, 12 */
     if ( *s > highest)
        highest = *s;            /* 5, 10 */
   return highest;               /* 13 */
}
Stepsni*shighestWhat Happens
180FADE085??55??Arguments Passed
280FADE085??5555highest Initialized
380FADE0C5??7455s Incremented
480FADE0C517455i Initialized
580FADE0C517474*s > highest; highest Replaced
680FADE0C527474i Incremented
780FADE10524574s Incremented
880FADE10534574i Incremented
980FADE14538874s Incremented
1080FADE14538888*s > highest; highest Replaced
1180FADE14548888i Incremented
1280FADE18546788s Incremented
1380FADE18546788Value of highest Returned

Back to Top

Pointer Notation with Offset

get_highest() could also be written using pointer notation with an offset. i is used as the offset for the pointer from the beginning of the array. Thus, when the value of i is 2, we are dealing with the element two elements from the initial one in the array. The value of the pointer offset changes so that each element is pointed at in succession. N.B. The offset does not represent the number of bytes from the beginning, but rather the number of array elements from the beginning.

int get_highest(int *s, int n)  /* 1 */
/* Assumes that there is at least one element */
{
   int i;
   int highest = *s;            /* 2 */

   for( i = 1; i < n; i++)      /* 3, 5, 6, 8 */ 
     if ( *(s + i) > highest)
        highest = *(s + i);     /* 4, 7 */ 
   return highest;              /* 9 */
}
Steps*snis + i*(s + i)highestWhat Happens
180FADE08555????????Arguments Passed
280FADE08555??????55highest Initialized
380FADE08555180FADE0C7455i Initialized
480FADE08555180FADE0C7474*(s + 1) > highest; highest Replaced
580FADE08555280FADE104574i Incremented
680FADE08555380FADE148874i Incremented
780FADE08555380FADE148888*(s + 3) > highest; highest Replaced
880FADE08555480FADE186788i Incremented
980FADE08555480FADE186788Value of highest Returned

Back to Top

Sample Program

This is a program which initializes an array of integer scores with 5 values and then calls a function to find, return, and print the highest score.

#include <stdio.h>

int get_highest(int s[], int n);

int main()
{
   int scores[10] = {55, 74, 45, 88, 67};
   int num_scores = 5;
   int high_score;
   
   high_score = get_highest(scores, num_scores);

   printf("The highest score is %d.\n", high_score);

   return 0;
}

int get_highest(int s[], int n)
/* Assumes that there is at least one element */
{
   int i;
   int highest = s[0];

   for( i = 1; i < n; i++)
     if ( s[i] > highest)
        highest = s[i];
   return highest;
}

Back to Top


© 1999-2000 DFStermole
Created 12 Jan 1999
Revised 19 Jan 2000