Passing array to function in C programming with example (C 语言中数组作为参数传递的示例 )

Just like variables, array can also be passed to a function as an argument .

In this guide, we will learn how to pass the array to a function using call by value and call by reference methods.

 

1-D Array

Passing array to function using call by value method

As we already know in this type of function call, the actual parameter is copied to the formal parameters.

#include <stdio.h>
void disp(char ch)
{
   printf("%c ", ch);
}
int main()
{
   char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
   for (int x=0; x<10; x++)
   {
       /* I’m passing each element one by one using subscript*/
       disp (arr[x]);
   }

   return 0;
}

Output:

a b c d e f g h i j

Passing array to function using call by reference

When we pass the address of an array while calling a function then this is called function call by reference. When we pass an address as an argument, the function declaration should have a pointer as a parameter to receive the passed address.

#include <stdio.h>
void disp( int *num)
{
    printf("%d ", *num);
}

int main()
{
     int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
     for (int i=0; i<10; i++)
     {
         /* Passing addresses of array elements*/
         disp (&arr[i]);
     }

     return 0;
}

Output:

1 2 3 4 5 6 7 8 9 0

How to pass an entire array to a function as an argument?

In the above example, we have passed the address of each array element one by one using a for loop in C. However you can also pass an entire array to a function like this:

Note: The array name itself is the address of first element of that array. For example if array name is arr then you can say that arr is equivalent to the &arr[0].

#include <stdio.h>
void myfuncn(int *var1, int var2)
{
	/* The pointer var1 is pointing to the first element of
	 * the array and the var2 is the size of the array. In the
	 * loop we are incrementing pointer so that it points to
	 * the next element of the array on each increment.
	 *
	 */
    for(int x=0; x<var2; x++)
    {
        printf("Value of var_arr[%d] is: %d \n", x, *var1);
        /*increment pointer for next element fetch*/
        var1++;
    }
}

int main()
{
     int var_arr[] = {11, 22, 33, 44, 55, 66, 77};
     myfuncn(var_arr, 7);
     return 0;
}

Output:

Value of var_arr[0] is: 11 
Value of var_arr[1] is: 22 
Value of var_arr[2] is: 33 
Value of var_arr[3] is: 44 
Value of var_arr[4] is: 55 
Value of var_arr[5] is: 66 
Value of var_arr[6] is: 77 

 

2-D Array

This post is an extension of How to dynamically allocate a 2D array in C?

A one dimensional array can be easily passed as a pointer, but syntax for passing a 2D array to a function can be difficult to remember. One important thing for passing multidimensional arrays is, first array dimension does not have to be specified. The second (and any subsequent) dimensions must be given.

1) When both dimensions are available globally (either as a macro or as a global constant).

#include <stdio.h>

const int M = 3;

const int N = 3;

  

void print(int arr[M][N])

{

    int i, j;

    for (i = 0; i < M; i++)

      for (j = 0; j < N; j++)

        printf("%d ", arr[i][j]);

}

  

int main()

{

    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    print(arr);

    return 0;

}

Output:

1 2 3 4 5 6 7 8 9

2) When only second dimension is available globally (either as a macro or as a global constant).

#include <stdio.h>

const int N = 3;

  

void print(int arr[][N], int m)

{

    int i, j;

    for (i = 0; i < m; i++)

      for (j = 0; j < N; j++)

        printf("%d ", arr[i][j]);

}

  

int main()

{

    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    print(arr, 3);

    return 0;

}

Output:

1 2 3 4 5 6 7 8 9

The above method is fine if second dimension is fixed and is not user specified. The following methods handle cases when second dimension can also change. (二维数组的第二个维度不确定的情况)

3) If compiler is C99 compatible
From C99, C language supports variable sized arrays to be passed simply by specifying the variable dimensions (See this for an example run)

// The following program works only if your compiler is C99 compatible.

#include <stdio.h>

  

// n must be passed before the 2D array

void print(int m, int n, int arr[][n])

{

    int i, j;

    for (i = 0; i < m; i++)

      for (j = 0; j < n; j++)

        printf("%d ", arr[i][j]);

}

  

int main()

{

    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    int m = 3, n = 3;

    print(m, n, arr);

    return 0;

}

Output on a C99 compatible compiler:

1 2 3 4 5 6 7 8 9

If compiler is not C99 compatible, then we can use one of the following methods to pass a variable sized 2D array.

4) Using a single pointer
In this method, we must typecast the 2D array when passing to function.

#include <stdio.h>

void print(int *arr, int m, int n)

{

    int i, j;

    for (i = 0; i < m; i++)

      for (j = 0; j < n; j++)

        printf("%d ", *((arr+i*n) + j));

}

  

int main()

{

    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    int m = 3, n = 3;

  

    // We can also use "print(&arr[0][0], m, n);"

    print((int *)arr, m, n);

    return 0;

}

Output:

1 2 3 4 5 6 7 8 9

References:

https://beginnersbook.com/2014/01/c-passing-array-to-function-example/

https://www.geeksforgeeks.org/pass-2d-array-parameter-c/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值