Passing an array as an argument to a function in C

后端 未结 10 1247
太阳男子
太阳男子 2020-11-22 06:03

I wrote a function containing array as argument, and call it by passing value of array as follows.

void arraytest(int a[])
{
    // changed the array a
    a         


        
10条回答
  •  -上瘾入骨i
    2020-11-22 06:21

    If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.

    int func(int arr[], ...){
        .
        .
        .
    }
    
    int func(int arr[SIZE], ...){
        .
        .
        .
    }
    
    int func(int* arr, ...){
        .
        .
        .
    }
    

    So, you are modifying the original values.

    Thanks !!!

提交回复
热议问题