719 views
#include <stdio.h>
int foo(int a[100])
{
return sizeof(a);
}
int main()
{
int a[10];
printf("%d", foo(a));
}

What will be the output of the above code ignoring any compiler warnings and assuming sizeof(int) as 4 when run on a 64 bit machine?

| 719 views

In C programming language, array parameters are treated as pointers .So here we  are passing base address of array a through function foo().

So the expression sizeof(a) will become sizeof (int *) which results in 4 or 8 depending on machine 32 bit or 64 bit.

Here it is mentioned that on 64 bit machine so it will return 8.

by Boss (38.7k points)
selected by
0

@ManojK @Arjun sir

In question it is mentioned that

"ignoring any compiler warnings and assuming sizeof(int) as 4 when run on a 64 bit machine"

But in ans it is specified that

Here it is mentioned that on 64 bit machine so it will return 8.

As i know pointer size and integer size are same for any implementaition in C so 4byte is given here for int as specified in Question so for ( int * ) also it should be 4.

I M getting a contradiction here plz clarify :)

+4

As i know pointer size and integer size are same for any implementaition in C s

Just this statement is wrong :) I used the values from my machine and should be the same in 99% of IIT/IISc programming labs. 64 bit compiler means sizeof pointer is 8. Now, sizeof int is 4 only - because that is enough for most practical purpose and for larger ones we can use long int.

+2
Thanks sir can u plz clarify my one more doubt

diffrence between 64 bit

1.compiler (as u mentioned here)

2. OS

3.Architecture(cpu)
+9
They all interact with each other. So, for 64 bit compiler we need a 64 bit OS which in turn require a 64 bit CPU. But Intel machines support backward compatibility which allows us to use a 32 bit compiler/OS on 64 bit machines. But this is like using a BMW car for daily shopping :)
+10

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 !!!

0
sizeof(int) is 4B in 32-bit and 64-bit machine ?
Awesome ! I never checked it tough untill I see this question .
0
i have small doubt that base address is passed to the foo function and size of array in foo is (int a[100])  which is 100cells.so total size should be 100*4=400.am i right or wrong?
+2

In third example given by you

int a[10];
a[2]=2;

printf("%d ",a[60]); // prints 0 before calling foo    ,why this would print 0,it must give garbage value
0

I am also haiving same doubt

int a[10] mean we have allocated 10  Bytes from memory may or may not be consecutive

but a[60] how it give 0 ??

0
yes a[60] intial keep garbage value

+1 vote