C programming tutorials for Beginners – Pass arrays to a function in C

Hits: 73

(C Programming Tutorials)

Pass arrays to a function in C

In this tutorial, you’ll learn to pass arrays (both one-dimensional and multidimensional arrays) to a function in C programming with the help of examples.


Passing individual array elements

Passing array elements to a function is similar to passing variables to a function.


Example 1: Passing an array

#include <stdio.h>
void display(int age1, int age2){
    printf("%dn", age1);
    printf("%dn", age2);
}

int main(){
    int ageArray[] = {2, 8, 4, 12};

    // Passing second and third elements to display()
    display(ageArray[1], ageArray[2]); 
    return 0;
}

Output

8
4

Example 2: Passing arrays to functions

// Program to calculate the sum of array elements by passing to a function 

#include <stdio.h>
float calculateSum(float age[]);

int main(){
    float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};

    // age array is passed to calculateSum()
    result = calculateSum(age); 
    printf("Result = %.2f", result);
    return 0;
}

float calculateSum(float age[]){

  float sum = 0.0;

  for (int i = 0; i < 6; ++i) {
		sum += age[i];
  }

  return sum;
}

Output

Result = 162.50

To pass an entire array to a function, only the name of the array is passed as an argument.

result =  calculateSum(age);

However, notice the use of [] in the function definition.

float calculateSum(float age[]){
... ..
}

This informs the compiler that you are passing a one-dimensional array to the function.

 


Passing Multidimensional Arrays to a Function

To pass multidimensional arrays to a function, only the name of the array is passed to the function(similar to one-dimensional arrays).

Example 3: Passing two-dimensional arrays

#include <stdio.h>
void displayNumbers(int num[2][2]);
int main(){
    int num[2][2];
    printf("Enter 4 numbers:n");
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
            scanf("%d", &num[i][j]);

    // passing multi-dimensional array to a function
    displayNumbers(num);
    return 0;
}

void displayNumbers(int num[2][2]){
    printf("Displaying:n");
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
           printf("%dn", num[i][j]);
        }
    }
}

Output

Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

 

C programming tutorials for Beginners – C Multidimensional Arrays

Python tutorials for Business Analyst – Python pass statement

C++ for Beginners: Passing Array to a Function in C++ Programming