DEV Community

abdullahriaz008
abdullahriaz008

Posted on

3 1

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.

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

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

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay