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 2 of 3.

Maya said:   1 decade ago
@nirlep:
It is given in the question. that int is of 4 bytes, so multiply by 4 and not 2

Neeraj said:   1 decade ago
The array has 5 elements and and size of each element is 4 bytes, so size of array is 5*4=20 bytes. So sizeof(arr)=20.

*arr is a pointer whose size is 4 bytes in LINUX, GCC(32 bit compiler) and 2 bytes in turbo c, c++(16 bit compiler).

The third one is simple.

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

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

What would sizeof(arr) return ?

Divya said:   1 decade ago
Thank you neeraj.

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.

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.

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.

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.

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

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


Post your comments here:

Your comments will be displayed after verification.