please explain the output of this code int a[10]; int *p=a; printf("%d%d%d",sizeof(a),sizeof(a[10]),sizeof(p)); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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));

5th Mar 2017, 12:38 PM
mrinalini
mrinalini - avatar
8 Answers
+ 2
@mrinalini A is array of int. Data type of element of A array is int. Sizeof int = 4
5th Mar 2017, 3:36 PM
SUPER_S
SUPER_S - avatar
+ 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.
5th Mar 2017, 3:41 PM
SUPER_S
SUPER_S - avatar
+ 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
5th Mar 2017, 12:55 PM
SUPER_S
SUPER_S - avatar
+ 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
5th Mar 2017, 5:13 PM
SUPER_S
SUPER_S - avatar
0
@SUPER_S please explain the 2 part of output i.e sizeof(a[10]) ...how this answer is 4
5th Mar 2017, 3:24 PM
mrinalini
mrinalini - avatar
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
5th Mar 2017, 3:40 PM
mrinalini
mrinalini - avatar
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
5th Mar 2017, 3:49 PM
mrinalini
mrinalini - avatar
0
Gdgs
20th Nov 2022, 10:41 AM
Fathima Abbas
Fathima Abbas - avatar