C - Passing an array to a function

Introduction

The function must be prototyped with the array specified as one of the arguments.

It looks like this:

void whatever(int nums[]); 

This statement prototypes the whatever() function.

That function accepts the integer array nums as its argument.

When you call a function with an array as an argument, you must omit the square brackets:

whatever(values); 

whatever() function is called using the array values as an argument.

The following code has the showarray() function that accepts an array as an argument.

Demo

#include <stdio.h>

#define SIZE 5 //from  w  ww .j a  v  a  2s . c om

void showarray(int array[]);

int main()
{
  int n[] = { 1, 2, 3, 5, 7 };

  puts("Here's your array:");
  showarray(n);
  return(0);
}

void showarray(int array[])
{
  int x;

  for (x = 0; x<SIZE; x++)
    printf("%d\t", array[x]);
  putchar('\n');
}

Result

Related Topic