0
please explain the output of this code int a[10]; int *p=a; printf("%d%d%d",sizeof(a),sizeof(a[10]),sizeof(p));
8 Réponses
+ 2
@mrinalini
A is array of int. Data type of element of A array is int. Sizeof int = 4
+ 2
The compiler does not check the size of the array or value of A[n]. He takes a data type from it's declaration.
+ 1
4044
40-4-4
40 = sizeof int * 10 = 4 * 10
4 = sizeof int
4 = sizeof int* = sizeof &int
The address value is automatically cast to type int. Sizeof int = 4
+ 1
int A[0]; // array with no element
int index;
cin >> index;
cout << A[index];
This code will compile. The compiler does not check the existence of the element. It will be a runtime error, not compile error
0
@SUPER_S please explain the 2 part of output i.e
sizeof(a[10]) ...how this answer is 4
0
you mean to say that sizeof(a[10]) gives the size of 10 element of array so it gives output 4 as array is of int type....and one more thing i want to clear that index is from 0 to 9 and we asking about size of 10 index then also why the output is showing because indexoutofbound exception should raise
0
sorry i am not getting it why sizeof(a[10]) does not show error because we are trying to get the size of array which is out of bound :p
0
Gdgs