OneBite.Dev - Coding blog in a bite size

get the nth element of array in C

Code snippet on how to get the nth element of array in C

int getNthElement(int array[], int n) {
  return array[n];
}

This short snippet of code provides a basic function for getting the nth element of any given array. First, the function requires two parameters- the array, and the nth element of the array that we are looking for. This function returns an integer, which would be the nth element of the array. The function then simply returns the element at the nth index of the array, which is found in the first line. Thus, this simple code allows us to look up and get the nth element of an array quickly and easily.

c