Introduction to Arrays in C

An array is a list of more than one variable having the same name.  Each variable in an array is known as an array element. We differentiate array elements by a subscript, which is  a number inside brackets. All array subscripts begin at zero, not one.

We can control individual elements in an array by their subscripts.

#include <stdio.h>

int main(void){

    int intArray[5];

    intArray[0] = 42;
    intArray[1] = 73;
    intArray[2] = 90210;
    intArray[3] = 64111;
    intArray[4] = 1138;


    printf("intArray[2] = %d\n", intArray[2]);
    printf("intArray[4] = %d\n", intArray[4]);


    return 0;

}

We can define an array as any data type in C. We can have integer arrays, long integer arrays, double arrays, char arrays, and so on. The compiler recognizes that we are defining an array, and not a single non-array variable, when we put brackets after the array’s name.

#include <stdio.h>

int main(void){

    
    char arrayChar[6];
    
    arrayChar[0]='H';
    arrayChar[1]='e';
    arrayChar[2]='l';
    arrayChar[3]='l';
    arrayChar[4]='o';
    arrayChar[5]='!';


    int i = 0;

    while(i<7){
        putchar(arrayChar[i++]);
    }



    return 0;

}


When we define an array, we tell the compiler to reserve a specific number of memory locations for that array by setting how many elements are in the array. Each element in an array uses the same amount of storage as a variable of the same type. C stores all array elements in a contiguous block.

#include <stdio.h>

int main(void){
 
  int n;
  int intArray[5];
 
  double j;
  double dubArray[5];
 
  printf("Size of a single int variable: %lu\n",
     sizeof(n));
 
  printf("Size of the intArray: %lu\n", sizeof(intArray));
 
  printf("Size of a single double variable: %lu\n",
     sizeof(j));
  printf("Size of a the dubArray: %lu\n", sizeof(dubArray));
 
  return 0;
 
}

We need to be sure to keep our subscript values within the range that we set when we declared the array.

We can create a string by initializing a char array with a character string. Note that with a string, we need an extra array element to hold the null zero at the end of the quoted string. A string is really just a char array where the last element is a null zero.

#include <stdio.h>

int main(void){
 
  char state1[15]="Missouri";
  char state2[15]="Nebraska";
 
  printf("%s and %s",
     state1, state2);
 
  char state3[15]={'K', 'a', 'n', 's', 'a', 's', ''};
 
  printf("\nand %s\n", state3);
 
  return 0;
 
}

We should always define an array with the maximum number of desired elements indicated in the subscript, unless we initialize the array at the same time.

#include <stdio.h>

int main(void){
 
  int nums[] = {1,3,5,7,11,13,17};
  long long bigNums[] = {8675309, 6647276};
 
  printf("%lld\n", bigNums[0]);
  printf("%d\n", nums[3]);
 
 
  return 0;
 
}

Our final example is a program to calculate the average of a set of grades given as double float values.

#include <stdio.h>

void printScores(double array[], int size);

int main(void){
 
  char uName[] = "Camden College";
 
  double grades[7] = {97.0, 81.3, 100.0, 89.9, 75.8, 88.5, 93.9};
 
  double averageGrade=0.0;
 
  int i;
 
 
  printScores(grades, 7);
 
  for(i=0; i<7; i++){
    averageGrade+=grades[i];
  }
 
  averageGrade/=7.0;
 
 
  printf("At %s, your average is %.1f\n",
     uName, averageGrade);
 
  return 0;
 
}

void printScores(double array[], int size){
  printf("Here are your grades:\n");
 
  int i;
  for(i=0;i<size;i++){
    printf("%.1f\n", array[i]);
  }
 
}

Note that to pass an array to a function, we need to specify only its name. In the function’s parameter list, we need to state the array type and include brackets after the array name.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Advertisements