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.
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;
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:
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.
| Variables | Values | Subscripts | Addresses |
| high_score | ?? | 80FADE00 | |
| num_scores | 5 | 80FADE04 | |
| scores | 55 | 0 | 80FADE08 |
| 74 | 1 | 80FADE0C | |
| 45 | 2 | 80FADE10 | |
| 88 | 3 | 80FADE14 | |
| 67 | 4 | 80FADE18 | |
| ?? | 5 | 80FADE1C | |
| ?? | 6 | 80FADE20 | |
| ?? | 7 | 80FADE24 | |
| ?? | 8 | 80FADE28 | |
| ?? | 9 | 80FADE2C |
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 */
}
| Step | s | n | i | s[i] | highest | What Happens |
| 1 | 80FADE08 | 5 | ?? | ?? | ?? | Arguments Passed |
| 2 | 80FADE08 | 5 | ?? | ?? | 55 | highest Initialized |
| 3 | 80FADE08 | 5 | 1 | 74 | 55 | i Initialized |
| 4 | 80FADE08 | 5 | 1 | 74 | 74 | s[1] > highest; highest Replaced |
| 5 | 80FADE08 | 5 | 2 | 45 | 74 | i Incremented |
| 6 | 80FADE08 | 5 | 3 | 88 | 74 | i Incremented |
| 7 | 80FADE08 | 5 | 3 | 88 | 88 | s[3] > highest; highest Replaced |
| 8 | 80FADE08 | 5 | 4 | 67 | 88 | i Incremented |
| 9 | 80FADE08 | 5 | 4 | 67 | 88 | Value of highest Returned |
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 */
}
| Step | s | n | i | *s | highest | What Happens |
| 1 | 80FADE08 | 5 | ?? | 55 | ?? | Arguments Passed |
| 2 | 80FADE08 | 5 | ?? | 55 | 55 | highest Initialized |
| 3 | 80FADE0C | 5 | ?? | 74 | 55 | s Incremented |
| 4 | 80FADE0C | 5 | 1 | 74 | 55 | i Initialized |
| 5 | 80FADE0C | 5 | 1 | 74 | 74 | *s > highest; highest Replaced |
| 6 | 80FADE0C | 5 | 2 | 74 | 74 | i Incremented |
| 7 | 80FADE10 | 5 | 2 | 45 | 74 | s Incremented |
| 8 | 80FADE10 | 5 | 3 | 45 | 74 | i Incremented |
| 9 | 80FADE14 | 5 | 3 | 88 | 74 | s Incremented |
| 10 | 80FADE14 | 5 | 3 | 88 | 88 | *s > highest; highest Replaced |
| 11 | 80FADE14 | 5 | 4 | 88 | 88 | i Incremented |
| 12 | 80FADE18 | 5 | 4 | 67 | 88 | s Incremented |
| 13 | 80FADE18 | 5 | 4 | 67 | 88 | Value of highest Returned |
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 */
}
| Step | s | *s | n | i | s + i | *(s + i) | highest | What Happens |
| 1 | 80FADE08 | 55 | 5 | ?? | ?? | ?? | ?? | Arguments Passed |
| 2 | 80FADE08 | 55 | 5 | ?? | ?? | ?? | 55 | highest Initialized |
| 3 | 80FADE08 | 55 | 5 | 1 | 80FADE0C | 74 | 55 | i Initialized |
| 4 | 80FADE08 | 55 | 5 | 1 | 80FADE0C | 74 | 74 | *(s + 1) > highest; highest Replaced |
| 5 | 80FADE08 | 55 | 5 | 2 | 80FADE10 | 45 | 74 | i Incremented |
| 6 | 80FADE08 | 55 | 5 | 3 | 80FADE14 | 88 | 74 | i Incremented |
| 7 | 80FADE08 | 55 | 5 | 3 | 80FADE14 | 88 | 88 | *(s + 3) > highest; highest Replaced |
| 8 | 80FADE08 | 55 | 5 | 4 | 80FADE18 | 67 | 88 | i Incremented |
| 9 | 80FADE08 | 55 | 5 | 4 | 80FADE18 | 67 | 88 | Value of highest Returned |
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;
}