loading...

Passing array to function in c programming

abdullahriaz008 profile image abdullahriaz008 ・2 min read

Arrays are a very important concept in c programming. Everyone knows why arrays are used and how to use it. And everyone also knows about different types of arrays in c programming.

  1. Linear Arrays
  2. Multidimensiona Arrays

But I have seen that new student who is learning c programming language are very confused how arrays pass to the functions. So today I'm here to tell you how we can pass an array to function in c programming.

Let's start,

Suppose we have an integer array of size 5.

int arr[5]={3,4,5,6,7};

and have a function print which can accept an array of integer. So we write something new in the prototype.

void print(int arr[]){

int i;

for(i=0;i<5;i++){
    printf("%d ",arr[i]);
}

}

We can send our array from our main function to the function we write. Just by passing the name of the array.

print(arr);

Because array name is the address of the first element in the array and we just need the address of the first element and we can iterate other by just increasing the index.

Also, read pointers in c.

Discussion

markdown guide
 

I used prototype as main and can change it now.Actually the loop in main is hard