How Array As Function Argument in C/C++

Share Your Love

When we send an array to a method like fun() in C, fun() always treats it as a pointer. The following example demonstrates this.

In C:++

#include <iostream>
using namespace std;
// 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]);
cout <<"\nArray size inside fun() is "<< n;
}
// Driver program
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;
}
// This code is contributed by shivanisinghss2110

Output:

prog.cpp: In function ‘void fun(int*)’:
prog.cpp:8:28: warning: ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int*’ [-Wsizeof-array-argument]
 unsigned int n = sizeof(arr)/sizeof(arr[0]);
                            ^
prog.cpp:6:18: note: declared here
 void fun(int arr[]) // SAME AS void fun(int *arr)
                  ^
Array size inside main() is 8
Array size inside fun() is 2

In C:

#include <stdio.h>
#include <stdlib.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;
}

Output:

prog.c: In function ‘fun’:
prog.c:7:24: warning: ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int *’ [-Wsizeof-array-argument]
 unsigned int n = sizeof(arr)/sizeof(arr[0]);
                        ^
prog.c:5:14: note: declared here
 void fun(int arr[]) // SAME AS void fun(int *arr)
              ^
Array size inside main() is 8
Array size inside fun() is 2

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:

In C++:

#include <iostream>
using namespace std;
void fun(int *arr, unsigned int n)
{
int i;
for (i = 0; i < n; i++)
	cout <<" "<< 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;
}
// This code is contributed by shivanisinghss2110

Output:

1  2  3  4  5  6  7  8

In C:

#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;
}

Output:

1  2  3  4  5  6  7  8

Some Exercise:

Predict the output of the below C programs:

1.

In C++:

#include <iostream>
using namespace std;
void fun(int arr[], unsigned int n)
{
int i;
for (i = 0; i < n; i++)
	cout<< 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;
}
// This code is contributed by shivanisinghss2110

Output:

1 2 3 4 5 6 7 8

In C:

#include <stdio.h>
void fun(int arr[], unsigned int n)
{
int i;
for (i=0; i<n; i++)
	printf("%d ", arr[i]);
}
// Driver rogram
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

2.

In 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];
}
// Driver program
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
fun(arr);
return 0;
}
// This code is contributed by shivanisinghss2110

Output:

1 2 

In 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]);
}
// Driver program
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
fun(arr);
return 0;
}

Output:

1 2 

3.

In 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] <<" ";
}
// Driver program
int main()
{
char arr[] = "geeksquiz";
fun(arr);
return 0;
}
// This code is contributed by shivanisinghss2110

Output:

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

In 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]);
}
// Driver program
int main()
{
char arr[] = "geeksquiz";
fun(arr);
return 0;
}

Output:

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

4.

In C++:

#include <iostream>
#include <bits/stdc++.h>
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];
}
// Driver program
int main()
{
char arr[] = {'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z'};
fun(arr);
return 0;
}
// this code is contributed by shivanisinghss2110

Output:

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

In 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]);
}
// Driver program
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  @

The character array in the preceding program is not terminated with a ‘0’.

Now, these are some popular techniques that we utilise, but did you know that there is a super cool way to do the same that allows you to keep an array’s identity? In a bit, we’ll talk about it briefly.

To do so, we must first consider the disadvantages of all of the above-mentioned strategies

Drawbacks:

  • The compiler has no idea what you’re passing, which is a huge disadvantage of the above method. What I mean is that we’re just passing an int* to the compiler, and we know it’s pointing to an array, but the compiler doesn’t.
  • You may test my claim by running a for-each loop on your array. You will almost certainly receive an error message stating that no callable begin or end function could be identified.
  • This is due to the fact that supplying an array is equivalent to passing an integer pointer, and as it contains no information about the underlying array, no iterator is provided.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter