CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
Can the sizeof operator be used to tell the size of an array passed to a function?



Category C Interview Questions
Rating (5.0) By 25 users
Added on 7/19/2006
Views 1941
Rate it!
Answers:

No. There?s no way to tell, at runtime, how many elements are in an array parameter just by looking at the
array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to
the first element.



In VC++, this is wat i tried

main()
{
int a[10];
printf("%d", sizeof(a);
}

Output: 40

This shows dat the sizeof can be operated on arrays as well.



 Posted by: Datta    

Contact Datta  Contact Datta

20



 Posted by: hemasundar    

Contact hemasundar  Contact hemasundar

yes



 Posted by: hemasundar    

Contact hemasundar  Contact hemasundar

yes



 Posted by: hemasundar    

Contact hemasundar  Contact hemasundar

GOD!!!! so many 'yes'...

The answer is NO.. Pass the array element to a function and see. The array is passed as a pointer (pointer to the begining address of the array)..



 Posted by: Vivek    

Contact Vivek  Contact Vivek

no,size of operator is compile time operator, it is used for determine the lenght of array and structure but in compile time not run time



 Posted by: VICKY SHRIVASTVA    

Contact VICKY SHRIVASTVA  Contact VICKY SHRIVASTVA

sizeof() is an operator, it is not a function, this is reason you are getting the answer out at 40.

Try the following:

# include <stdio.h>
# include <malloc.h>

void printf_sz(int a[])
{
printf(" Sizeof : %d", sizeof(a));
}



main()
{
int a[20];

printf (" Sizeof : %d", sizeof(a));

printf_sz(a);


}

The output of the above program is 80 and 4.

Check for yourself



 Posted by: Deoashish Dane    

Contact Deoashish Dane  Contact Deoashish Dane


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Is using exit() the same as using return?


View Answer
Is it possible to execute code even after the program exits the main() function?


View Answer
What is a static function?


View Answer
How do you print an address?


View Answer
What is the difference between NULL and NUL?


View Answer
What is the stack?


View Answer
What is a null pointer?


View Answer
How can I convert a number to a string?


View Answer
What is the difference between #include <file> and #include ?file??


View Answer
What is Preprocessor?


View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6071/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.8