Question:
Can the sizeof operator be used to tell the size of an array passed to a function?
Answer:
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. Source: CoolInterview.com
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. Source: CoolInterview.com
Answered by: Datta | Date:
| Contact Datta
20 Source: CoolInterview.com
Answered by: hemasundar | Date:
| Contact hemasundar
yes Source: CoolInterview.com
Answered by: hemasundar | Date:
| Contact hemasundar
yes Source: CoolInterview.com
Answered by: hemasundar | Date:
| 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).. Source: CoolInterview.com
Answered by: Vivek | Date:
| 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 Source: CoolInterview.com
Answered by: VICKY SHRIVASTVA | Date: 12/9/2009
| 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 Source: CoolInterview.com
Answered by: Deoashish Dane | Date: 12/22/2009
| Contact Deoashish Dane
Yes, there is a way -
#include<conio.h> #include<stdio.h>
int main() { int arr[] = {4, 2, 4, 5, 2, 3, 1}; int arr_size = sizeof(arr)/sizeof(arr[0]); printf("Array size = %d", arr_size); getchar(); return 0; } Source: CoolInterview.com
Answered by: Manan | Date: 6/19/2010
| Contact Manan
hey we can not calculate size of the array element we can only calculate the size of the datatype only try this headerfile void main() { clrscr(); int i; printf("the size of i is ",size of (i)); getch(); } Source: CoolInterview.com
Answered by: PARTHIBAN.V | Date: 7/21/2010
| Contact PARTHIBAN.V
No,It is not possible ..
#include<stdio.h> #include<conio.h>
void sourav(int a[]) { int p=sizeof(a); printf("%d",p); }
int main() { int a[10]; sourav(a); getch(); return 0; }
This program gives output 4.But it is not correct ,so it is not possible. Source: CoolInterview.com
Answered by: sourav | Date: 7/27/2010
| Contact sourav
how about useing the strlen() instead of size of it'll do more favout Source: CoolInterview.com
Answered by: erwer | Date: 7/30/2010
| Contact erwer
we can know about the no. of elements in an array by the help of "sizeof()" operator if and only if we know the data type and the total bytes hold by that array... Source: CoolInterview.com
Answered by: shashi kant pal | Date: 8/7/2010
| Contact shashi kant pal
yo shd do practicaly n clear here yo r not goin to get any thng Source: CoolInterview.com
Answered by: saurav | Date: 8/29/2010
| Contact saurav
#include<stdio.h> #include<conio.h>
void main() {
long int a[10]; clrscr(); printf("%d", sizeof(a)); getch(); } output: 20,in case of char output is 10 hey we can calculate size of the array element.which depends on the datatype only try this Source: CoolInterview.com
Answered by: jayant kumar | Date: 9/3/2010
| Contact jayant kumar
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|