Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of sizeof operator

Tags:

c

sizeof

The output of following program

#include<stdio.h>
int main(){
    int *p[10];
    printf("%ld %ld\n",sizeof(*p),sizeof(p));
}

is

8   <--- sizeof(*p) gives  size of single element in the array of int *p[10] 
80  <--- sizeof(p) gives  size of whole array which is 10 * 8 in size.

now see the following program

 #include<stdio.h>
 #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
 int array[] = {23,34,12,17,204,99,16};

  int main()
  {
      int d;
      printf("sizeof(array) = %ld \n",sizeof(array));
      printf("sizeof(array[0]) = %ld \n",sizeof(array[0]));
      printf("sizeof int %ld\n",sizeof(int));
      printf("TOTAL_ELEMENTS=%ld \n",TOTAL_ELEMENTS);
      for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
          printf("%d\n",array[d+1]);

      return 0;
  }

is

sizeof(array) = 28 
sizeof(array[0]) = 4  <--here
sizeof int 4
TOTAL_ELEMENTS=7 

What I am not able to understand is why is the sizeof(array[0]) different in both the outputs.

like image 679
Registered User Avatar asked Mar 26 '11 07:03

Registered User


People also ask

What is the use of sizeof () in C?

We generally use the sizeof() operator in the C language so that we can determine the total size of the data type or the expression that is specified in the storage units of char-size. The sizeof() operator consists of just one operand that can either be a cast data type or an expression.

What is the use of sizeof () operator in C ++? Give an example?

The sizeof() operator in C gives the size of its operand at compile time. It does not evaluate its operand. For example, int ar1[10]; sizeof(ar1) // output 40=10*4 sizeof(ar1[-1]) // output 4 int ar2[ sizeof(ar1) ]; // generate an array of 40 ints.

How does sizeof () work in C++?

The sizeof() operator is a function which returns the size of any data type, expression, array, etc. It takes the data type or expression as a part of argument which is mandatory and returns the result which is size of that data type in bytes. If it is an array it will return the number of elements present in it.

What is the use of sizeof operator in Java?

sizeOf() is a static method of the FileUtils class that is used to get the size of the specified file or directory in bytes. If the file provided is a regular file, then the file's length is returned. If the argument is a directory, then the size of the directory is calculated recursively.


2 Answers

int *p[10]; is an array of pointers.

*p is the first element of that array of pointers. So it is a pointer to an integer. It is not an integer.

int array[] = {23,34,12,17,204,99,16}; is an array of integers. So array[0] is the first element of that array. So it is an integer.

The size of a pointer to an integer (*p) and an integer (array[0]) are different.

So sizeof(*p) and sizeof(array[0]) are different.

sizeof(p) gives the size of the array of pointers. So it is: 10 x 8 = 80.
i.e. (number of elements) x (size of one element)

sizeof(array) gives the size of the array of integers. So it is: 7 x 4 = 28.

like image 181
Can't Tell Avatar answered Nov 04 '22 16:11

Can't Tell


In the first example, the element is a pointer to int, while in the second example it's just int. You can see that the pointer has 8 bytes, the int just 4 bytes.

like image 42
Lukáš Lalinský Avatar answered Nov 04 '22 17:11

Lukáš Lalinský