What is an array?

An array is a data structure that holds only fixed-size data and its contents are arranged in contiguous memory slots. The primary purpose of using an array is to store data items of the identical type together. An array can stockpile any one of the following types of data: int, float, char and so forth. The elements are stored from index 0, and the difference between the two indexes of the array is termed as an offset. The array has a fixed size. Once the size is defined during array declaration, it cannot be changed later.

Syntax used to declare an array: datatype arrayname[n];

  • datatype represents the type of data items stored in the array. For instance, it can be of type int, double, char, and so forth.
  • arrayname represents the name of the array. The array name must obey the identifier rules.
  • n represents the size of an array. It must be an integer value.

One can pass the whole array as an argument to a function. Passing an array to a function is identical to passing a variable as argument to a function.

What are arguments?

Arguments are values passed to a function that is used during function execution. The local variables (formal parameters) specified in the function header will accept the arguments (actual parameters) passed to it during the function call and the function body processes these arguments. The default value assigned to the arguments will be used when the number of arguments passed during the function call does not match the number of arguments in the function header. The number and type of arguments in the function call must match with the function definition.

3-Ways of passing an array as argument

If a 1-D array must be passed as an argument, one can use any of these three ways to declare it.

First way:- Formal parameter declared as a pointer which points to the first element of the array passed as an argument to the function during the function call.

int display(int *x) {   .   .   .}

Second way:- Formal parameter declared as an array of size equal to the size of the array passed as an argument to the function when the function is invoked.

int display(int x[10]) {   .   .   .}

Third way:- Formal parameter declared as an array without mentioning its size. In this case, the size of the array is determined at run time depending upon the size of the array passed during the function invocation.

int display(int x[]) {   .   .   .}

Passing arrays to a function in C

The following program demonstrates how to pass elements of an array individually as argument to a function in C programming language. The program declares an integer array and initializes its elements. It then iterates a loop and then accesses each element of the array using its index. It then passes each element of the array to the function. The function displays the element passed to it.

Example 1: Passing array elements as an argument to a function

#include <stdio.h>void showNum(int num1) {  printf("%dn", num1);}int main() {  int numArray[] = {12, 18, 14, 22};  int i;  int count;  count = sizeof(numArray)/sizeof(int);  for(i=0;i<count;i++)  showNum(numArray[i]);   return 0;}
#Output12181422

In the above code, The contents of an array are passed as parameter to the showNum() function just like passing a variable as argument to the function.

Example 2: Pass the entire array as parameter to a function in C

// Program to calculate the product of array elements by passing it to the function #include <stdio.h>int calculateMUL(int arraynum[]);int main() {  int result, arraynum[] = {51, 22, 30, 45, 20};  // arraynum is passed to calculateMUL()  result = calculateMUL(arraynum);   printf("Result = %d", result);  return 0;}int calculateMUL(int arraynum[]) {  int mul = 1;  for (int i = 0; i < 5; i++) {    mul = mul*arraynum[i];  }  return mul;}
#output30,294,000

Example 3: Passing a multidimensional array to a function in C

#include <stdio.h>void ArrayElement(int arr[3][3]);int main() {  int arr[3][3];  printf("Enter 9 numbers:n");  for (int i = 0; i < 3; ++i) {    for (int j = 0; j < 3; ++j) {      scanf("%d", &arr[i][j]);    }  }  // pass multi-dimensional array to a function  ArrayElement(arr);  return 0;}void ArrayElement(int arr[3][3]) {  printf("Display array elements:n");  for (int i = 0; i < 3; ++i) {    for (int j = 0; j < 3; ++j) {      printf("%dn", arr[i][j]);    }  }}
#outputEnter 9 numbers2345678910Display array elements2345678910

Common Mistakes

In C programming language, an array is passed by reference by default. Therefore, any changes made to the array in the called function is reflected in the calling function.

Context and Applications

This topic is critical within the professional exams for both undergraduate and graduate courses, especially for:

  • Bachelors in Computer Science
  • Masters in Computer Science
  • Bachelors in Information Technology
  • Single-Dimensional Arrays
  • Jagged Arrays
  • Arithmetic Operators

Practice Problems

Q1. What is an Array in C language?

  1. Collection of elements of the same data type.                          
  2. An array contains more than one element.
  3. Array elements are saved in consecutive memory locations.
  4. All the above mentioned.

Correct option: (4) All the above mentioned.

Explanation: Array is a data structure used to stock components of similar data types in successive memory locations.

Q2. Choose the proper assertion about C language Arrays.

  1. An array address is the base location of the first component of the array itself.
  2. Array size should be announced if not instantiated right away.
  3. Array size is the number of elements in the array.
  4. All the above mentioned.

Correct option: (4) All the above mentioned.

Explanation: The array address starts with the address of the first element. The size of the array express the number of elements in the array.

Q3. What is the index of the first array element?

  1. -1
  2. 1
  3. 0
  4. 2

Correct option: (3) 0

Explanation: Array index starts at 0.

Q4. What is the maximum number of elements an array in C might have?

  1. 2
  2. 8
  3. 16
  4. No restriction

Correct option: (4) No restriction

Explanation: Theoretically, no restriction. The maximum size of an array depends upon the available memory size and compilers.

Q5. An array is a collection of elements of __________.

  1. similar type
  2. composite type
  3. dissimilar type
  4. None of these

Correct option: (1) similar type

Explanation:-Array is a collection of elements of the same type.

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Function

Passing Array as Argument

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Function

Passing Array as Argument