Difference between sizeof(int *) and sizeof(int) in C/C++

sizeof() is commonly used operator in the C or C++. It is a compile-time unary operator that can be used to compute the size of its operand. The result of sizeof() is of unsigned integral type which is usually denoted by size_t. This operator can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as structure, union etc.

int means a variable whose datatype is integer.
sizeof(int) returns the number of bytes used to store an integer.

int* means a pointer to a variable whose datatype is integer.
sizeof(int*) returns the number of bytes used to store a pointer.

Since the sizeof operator returns the size of the datatype or the parameter we pass to it. So, the value it should return after passing a variable of (int *) to it:

Now, the value it should return after passing a variable of (int) to it:



Below is the illustration of sizeof operator on 64-bit machine:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate the
// sizeof operator
#include <stdio.h>
  
// Driver code
int main()
{
    // Print the sizeof integer
    printf("Size of (int) = %lu"
           " bytes\n",
           sizeof(int));
  
    // Print the size of (int*)
    printf("Size of (int*) = %lu"
           " bytes\n",
           sizeof(int*));
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate the
// sizeof operator
#include "bits/stdc++.h"
using namespace std;
  
// Driver Code
int main()
{
    // Print the sizeof integer
    cout << "Size of (int) = "
         << sizeof(int) << " bytes\n";
  
    // Print the size of (int*)
    cout << "Size of (int *) = "
         << sizeof(int*) << " bytes\n";
  
    return 0;
}
chevron_right

Output:
Size of (int) = 4 bytes
Size of (int*) = 8 bytes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :