C Feed Array to a Function

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name.

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

C++

#include <iostream>

using namespace std;

void fun( int arr[])

{

unsigned int n = sizeof (arr) / sizeof (arr[0]);

cout << "\nArray size inside fun() is " << n;

}

int main()

{

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

unsigned int n = sizeof (arr) / sizeof (arr[0]);

cout << "Array size inside main() is " << n;

fun(arr);

return 0;

}

C

#include <stdio.h>

#include <stdlib.h>

void fun( int arr[])

{

unsigned int n = sizeof (arr)/ sizeof (arr[0]);

printf ( "\nArray size inside fun() is %d" , n);

}

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;

}

Output

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

Therefore in C, we must pass the size of the array as a parameter. Size may not be needed only in the case of '\0' terminated character arrays, size can be determined by checking the end of string character.
Following is a simple example to show how arrays are typically passed in C:

C++

#include <iostream>

using namespace std;

void fun( int *arr, unsigned int n)

{

int i;

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

cout << " " << arr[i];

}

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;

}

C

#include <stdio.h>

void fun( int *arr, unsigned int n)

{

int i;

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

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

}

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;

}

Output

1  2  3  4  5  6  7  8

Exercise: Predict the output of the below C programs:
Program 1:

C++

#include <iostream>

using namespace std;

void fun( int arr[], unsigned int n)

{

int i;

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

cout << arr[i] << " " ;

}

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;

}

C

#include <stdio.h>

void fun( int arr[], unsigned int n)

{

int i;

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

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

}

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;

}

Output

1 2 3 4 5 6 7 8

 Program 2:

C++

#include <iostream>

using namespace std;

void fun( int * arr)

{

int i;

unsigned int n = sizeof (arr) / sizeof (arr[0]);

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

cout << " " << arr[i];

}

int main()

{

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

fun(arr);

return 0;

}

C

#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]);

}

int main()

{

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

fun(arr);

return 0;

}

Output

          1 2

According to the processor, size of pointer changes for  32 bit computer it assigns 4 bytes to pointer then output becomes 1.

Program 3:

C++

#include <iostream>

#include <string.h>

using namespace std;

void fun( char * arr)

{

int i;

unsigned int n = strlen (arr);

cout << "n = " << n << endl;

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

cout << arr[i] << " " ;

}

int main()

{

char arr[] = "geeksquiz" ;

fun(arr);

return 0;

}

C

#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]);

}

int main()

{

char arr[] = "geeksquiz" ;

fun(arr);

return 0;

}

Output

n = 9 g e e k s q u i z        

Program 4:

C++

#include <bits/stdc++.h>

#include <iostream>

using namespace std;

void fun( char * arr)

{

int i;

unsigned int n = strlen (arr);

cout << "n = " << n << "\n" ;

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

cout << " " << arr[i];

}

int main()

{

char arr[]

= { 'g' , 'e' , 'e' , 'k' , 's' , 'q' , 'u' , 'i' , 'z' };

fun(arr);

return 0;

}

C

#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]);

}

int main()

{

char arr[] = { 'g' , 'e' , 'e' , 'k' , 's' , 'q' , 'u' , 'i' , 'z' };

fun(arr);

return 0;

}

Output

n = 11 g e e k s q u i z

NOTE: The character array in the above program is not '\0' terminated. (See this for details)

Now These were some of the common approaches that we use but do you know that there is a better way to do the same. For this, we first need to look at the drawbacks of all the above-suggested methods:

Drawbacks:

  • A major drawback of the above method is compiler has no idea about what you are passing. What I mean here is for compiler we are just passing an int* and we know that this is pointing to the array but the compiler doesn't know this.
  • To verify my statement you can call for-each loop on your array. You will surely get an error saying no callable begin, end function found.
    This is because the passing array is like actually passing an integer pointer and it itself has no information about the underlying array hence no iterator is provided.

Template Approach (Reference to Array):

This method retains all information about the underlying array. This method is majorly based on reference to an array but using this with templates optimizes our method. Template dependency actually calculates the length of the array automatically at the time of function call so that it can be used to create a reference because a reference to an array must know the size of the array.

Here Template is used for template argument deduction.

C++

#include <iostream>

using namespace std;

template < size_t N> void print( int (&a)[N])

{

for ( int e : a) {

cout << e << endl;

}

}

int main()

{

int a[]{ 1, 2, 3, 4, 5 };

print(a);

}

Here you can see why we need template argument deduction. For a base to create a reference to an array so that we can take an array as a parameter.

Related Articles:

  • Do not use sizeof for array parameters.
  • Difference between pointer and array in C?

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


swineybrad1948.blogspot.com

Source: https://www.geeksforgeeks.org/how-arrays-are-passed-to-functions-in-cc/

0 Response to "C Feed Array to a Function"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel