C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 23)
23.
If the size of integer is 4bytes, What will be the output of the program?
#include<stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}
10, 2, 4
20, 4, 4
16, 2, 2
20, 2, 2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 1 of 3.

Anonimous said:   7 years ago
The answer will respect with bitwise.

If 16 bit then 10, 2, 2.

If 32bit then 20, 4, 4. Here we assume that it is 32bits.
(1)

Rohan said:   7 years ago
@All.

#include<stdio.h>

int main()
{
int arr[] = {12, 13, 14, 15, 16};
printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));

/* (i) sizeof(arr) -> there are 5 elements in the arr[] , and it contains integers and size of integer is 4 bytes...
-> 5 * 4 = 20 bytes
(ii) (*arr) -> it will return 12
sizeof(*arr) -> As , 12 is an integer of 4 bytes...

(iii) sizeof(arr[0])) -> size of first element of arr[] (i.e. 4 bytes)...

*/

return 0;
}
(4)

Dinga said:   7 years ago
Thank you @N Prathyusha.

Hemant said:   8 years ago
By size of array, it means the no of elements it can have rather than the complete size.

Haritha said:   10 years ago
Actually C language int datatype 2 bytes. So 2*5 elements then 10, and array size 2 and first element size of array is 2. So 10, 2, 2.

Iceberg said:   1 decade ago
*arr is same as arr[0], coz *arr is *(arr+0), So it is the size of int. Not the size of pointer. Try printing *arr, it will print the first element.

And if you make it a char array, it will print:

5,1,1

Where 5 is 1*5.

And rest 1 are size of char not the char pointers.

Raghavendra bhat said:   1 decade ago
There are 5 elements in an array. Each having size of 4 bytes.

Hence ans 20, ii)*ptr = declaration size of each array element=4,

iii)Array size of arr(0) = 4 bytes.
Hence answer would be 20, 4, 4.

Thank you.

Suma said:   1 decade ago
The no of elements in the array are 5, each one size is 4 bytes.

So 4*5=20, and arr[0], *arr are same.. first element size is 4 bytes.

Divya said:   1 decade ago
Thank you neeraj.

Jignesh said:   1 decade ago
What if the array was declared as

int *arr[] = { 1,2,3,4,5 } ;

What would sizeof(arr) return ?


Post your comments here:

Your comments will be displayed after verification.