Hi,
I want to know the size on an integer.
I know how to do this for a string but when i use this for integer with while look it stops at 0 number.
Any idea for the same?
Printable View
Hi,
I want to know the size on an integer.
I know how to do this for a string but when i use this for integer with while look it stops at 0 number.
Any idea for the same?
int[] array isn't an object, so the size isn't readily available (unless you make use of a datastructure like a vector).
Luckily there is the sizeof() operator. Here sizeof() returns the amount of bytes such an array occupies, when we dived this by the size of one unit (sizeof(int)) of the array we get the number of elements in it. (or at least the number of elements we reserver the room for)
Try this!
Code:#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[2];
/* Get the consecutive memory addresses */
unsigned long b = &a[0];
unsigned long c = &a[1];
/* Find the difference between the addresses */
unsigned int size = labs(b-c);
printf("Size of int is %u \n", size);
return 0;
}
When the parameter is a datatype.
For Eg:
Code:sizeof(int), sizeof(double)
#define GetSize(x) (char*)((x*)10 + 1) - (char*)10
When the parameter is a variable.
For Eg:
Code:int a;
float b;
sizeof(a), sizeof(b)
#define GetSize(x) (char*)(&x + 1) - (char*)&x
It will work for any data typeCode:#include <stdio.h>
struct node {
int x;
int y;
};
unsigned int find_size ( void* p1, void* p2 )
{
return ( p2 - p1 );
}
int main ( int argc, char* argv [] )
{
struct node data_node;
int x = 0;
printf ( "\n The size :%d",
find_size ( (void*) &data_node,
(void*) ( &data_node +
1 ) ) );
printf ( "\n The size :%d", find_size ( (void*) &x,
(void*) ( &x + 1 ) ) );
}