C Programming - Arrays - Discussion
Discussion Forum : Arrays - Yes / No Questions (Q.No. 3)
3.
Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);
int fun(int arr[]);
int fun(int arr[2]);
Answer: Option
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.
Discussion:
14 comments Page 1 of 2.
Anomi said:
8 years ago
In fact, they are same, representing variable array but the second alerts people it's a fixed-size array.
Rohit Singh said:
9 years ago
You are right @Ravindra Bagale.
Raj said:
9 years ago
1st one does not contain size of the array.
2nd one contain size of array.
2nd one contain size of array.
MrMin said:
10 years ago
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.
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.
Ankit Jain said:
1 decade ago
#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.
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.
Ajay said:
1 decade ago
#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?
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?
Ravindra bagale said:
1 decade ago
@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.
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.
Vijay said:
1 decade ago
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 ?
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 ?
Raj said:
1 decade ago
Thanks niyati nice explanation.
Maikal said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers