C Programming - Arrays - Discussion

3. 

Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);

[A]. Yes
[B]. No

Answer: Option B

Explanation:

No, both the statements are same. It is the prototype for the function fun() that accepts one integer array as an parameter and returns an integer value.


Bhavesh said: (Jun 13, 2011)  
The first statement signifies that any number of elements are possible in the array ?

Arun said: (Jul 22, 2011)  
You can pass an array of any size in d first one, where as in the 2nd one you can atmost pass an array of size 2.

Rahul said: (Sep 5, 2011)  
According to Comments given by @Bhavesh and @Arun how is it possible that both are same statements,bcoz the size is different for array in the declarations.

Niyati said: (Nov 16, 2011)  
I am satisfied with Arun,in 1st function declaration there is no specification of the size of array it accepts i.e. it can take an array of any size, but in second function declaration there is size of array specified so it can accept the array of atmost size 2,then How can these two statements be same

Maikal said: (Dec 20, 2011)  
Thanks niyati, your question is very intresting but can you say me what will be the fix size in int arr[2]& int arr[] when int is not define. So its are same because thease are prototype function.

Raj said: (Dec 25, 2011)  
Thanks niyati nice explanation.

Vijay said: (Jan 2, 2012)  
In give example,
int fun(int arr[]) is the passing the base element of an array & on other hand int fun(int arr[2]) is passing the third element of array. then why they r equal ?

Ravindra Bagale said: (Jul 3, 2012)  
@All.

Hello friends, this is just a declaration.
Declaration is all about just knowing the data type.
It is enough to know that, it is an array of int.
Subscript doesn't matter here.

int fun (int arr[]) ;
int fun (int arr[]) ;

Same.

int fun (int arr[]) ;
int fun (int arr[2]) ;

Same.

int fun (int arr[2]) ;
int fun (int arr[5]) ;

Same.

Ajay said: (May 13, 2013)  
#include<stdio.h>
int main()
{
int arr[5],i;
for(i=0; i<=10; i++)
{
arr[i]=i;
}
return 0;
}

You can see, we don't reach beyond the array size, is entirely the programmer's botheration not the compiler's.

How can you say that both are same please explain me?

Ankit Jain said: (Jun 27, 2013)  
#include<stdio.h>
int func(int arr[5]);

int main()
{
int arr[10],i;
for(i=0; i<10; i++)
{
arr[i]=i;
}
func(arr);
return 0;
}
int func(int arr[5])
{
int i;
for(i=0; i<10; i++)
{
printf("%d ",arr[i]);
}
}


O/P:
0 1 2 3 4 5 6 7 8 9

Therefore Answer is : same.

used gcc compiler.

MrMin said: (Jan 25, 2016)  
Types of pointers to these functions will vary, So you these declarations are NOT the same. Also, you can't do.

int func1 (int array [], int some_other);

But you can do int func2 (int array [2], int some_other);

That's because at lowest level the compiler needs to know where some_other argument lies on the call stack. With the first declaration it can't determine it in most cases.

Raj said: (Jun 25, 2016)  
1st one does not contain size of the array.

2nd one contain size of array.

Rohit Singh said: (Aug 20, 2016)  
You are right @Ravindra Bagale.

Anomi said: (Mar 26, 2017)  
In fact, they are same, representing variable array but the second alerts people it's a fixed-size array.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.