DEV Community

abdullahriaz008
abdullahriaz008

Posted on

Passing array to function in c programming

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.

Top comments (1)

Collapse
 
trungduong profile image
hoai_thanh pham

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