How arrays are passed to functions in C/C++

In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun(). Below example demonstrates the same.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
// Note that arr[] for fun is just a pointer even if square
// brackets are used
void fun(int arr[])  // SAME AS void fun(int *arr)
{
   unsigned int n = sizeof(arr)/sizeof(arr[0]);
   printf("\nArray size inside fun() is %d", n);
}
  
// Driver program
int main()
{
   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
   unsigned int n = sizeof(arr)/sizeof(arr[0]);
   printf("Array size inside main() is %d", n);
   fun(arr);
   return 0;
}

chevron_right


Output:

Array size inside main() is 8
Array size inside fun() is 1

Therefore in C, we must pass size of array as a parameter. Size may not be needed only in case of ‘\0’ terminated character arrays, size can be determined by checking end of string character.



Following is a simple example to show how arrays are typically passed in C.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
void fun(int *arr, unsigned int n)
{
   int i;
   for (i=0; i<n; i++)
     printf("%d  ", arr[i]);
}
  
// Driver program
int main()
{
   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
   unsigned int n = sizeof(arr)/sizeof(arr[0]);
   fun(arr, n);
   return 0;
}

chevron_right


Output:

1  2  3  4  5  6  7  8

Exercise:

Predict the output of below C programs:

  1. filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h> 
    void fun(int arr[], unsigned int n) 
    int i; 
    for (i=0; i<n; i++) 
        printf("%d ", arr[i]); 
      
    // Driver program 
    int main() 
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; 
    unsigned int n = sizeof(arr)/sizeof(arr[0]); 
    fun(arr, n); 
    return 0; 
    }

    chevron_right

    
    

  2. filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
    void fun(int *arr)
    {
       int i;
       unsigned int n = sizeof(arr)/sizeof(arr[0]);
       for (i=0; i<n; i++)
         printf("%d  ", arr[i]);
    }
      
    // Driver program
    int main()
    {
       int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
       fun(arr);
       return 0;
    }

    chevron_right

    
    

  3. filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
    #include <string.h>
      
    void fun(char *arr)
    {
       int i;
       unsigned int n = strlen(arr);
       printf("n = %d\n", n);
       for (i=0; i<n; i++)
         printf("%c  ", arr[i]);
    }
      
    // Driver program
    int main()
    {
       char arr[] = "geeksquiz";
       fun(arr);
       return 0;
    }

    chevron_right

    
    

  4. filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
    #include <string.h>
      
    void fun(char *arr)
    {
       int i;
       unsigned int n = strlen(arr);
       printf("n = %d\n", n);
       for (i=0; i<n; i++)
         printf("%c  ", arr[i]);
    }
      
    // Driver program
    int main()
    {
       char arr[] = {'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z'};
       fun(arr);
       return 0;
    }

    chevron_right

    
    

  5. Note that the character array in above program is not ‘\0’ terminated. See this for details.

    Related Articles:

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



    My Personal Notes arrow_drop_up

    Improved By : RishabhPrabhu