I really loved this question .
Some IMP 4 observation here I want to share with my friends is:-
1. if we replace foo defination by
int foo(int a[1]) or
int foo(int a[100]) or
int foo(int a[]) all are exactly same as
int foo(int *a)
2.sizeof operator
#include <stdio.h>
void foo(int a[100])
{
int b[100];
printf("%d",sizeof(b)); //returns 400 {each int takes 4Byte}
printf("%d",sizeof(a));//returns 4 byte if 32-bit compiler , 8Byte if 64-bit compiler
//Some may expect 400 as o/p .Reason the foo defination is exactly same as int foo(int *a) //and a is int pointer inside foo
}
int main(void)
{
int a[10];
printf("%d",sizeof(a)); // o/p 40 eventhough the same code given 4 as o/p inside foo (32-bit compiler) //bcz a is an array not a pointer here
foo(a);
return 0;
}
3.Bcz foo catches array a inside pointer whatever changes we made in foo reflects to main() and vice versa.
#include <stdio.h>
void foo(int a[100]) //exactly same as int foo(int *a)
{
printf("%d",a[2]);//o/p 2
a[60]=500;
}
int main(void)
{
int a[10];
a[2]=2;
printf("%d ",a[60]); // prints 0 before calling foo
foo(a);
printf("%d ",a[60]);//Though array size u declared 10 still u able to print a[60] (o/p 500) here cz foo catch array parameter inside pointer
//Also see Observation 4
return 0;
}
4.In c,c++ array never do bound check (Unsafe programming :->>we should not do this) but java do.(Bcz C wants to be faster with cost of security)
int main(void)
{
int a[10];
a[60]=60;
printf("%d ",a[60]);//prints 60
return 0;
}
PS:- All above codes i verified with diffrent compilers
Still verify and correct me if anywhere i have gone wrong !!!