让我们假设我已经声明了某些数据类型的变量'i'(可能是int,char,float或double的形式...)>
NOTE:
只需考虑声明了'i',如果它是int或char或float或double数据类型,就不要打扰。因为我想要一个通用的解决方案,所以我只是提到变量'i'可以是int,char,float或double的任何一种数据类型。现在可以在没有sizeof运算符的情况下找到变量'i'的大小吗?
让我们假设我已经声明了某些数据类型的变量'i'(可能是int,char,float或double)...注:只需考虑声明'i',如果它是int或char就不用理会或...
您可以使用以下宏,取自here:
下面的语句将给出通用的解决方案:
自从我编写任何C代码以来已经很久了,我从来都不擅长,但是看起来不错:
这有效..
int *a, *b,c,d;/* one must remove the declaration of c from here*/
int c=10;
a=&c;
b=a;
a++;
d=(int)a-(int)b;
printf("size is %d",d);
我希望下面的代码能在不使用sizeof()运算符的情况下解决c ++中的问题
尝试一下,
这应该给您变量的大小
无需使用sizeof
运算符即可查找变量大小的程序
#include<stdio.h>
#include<conio.h>
struct size1
{
int a;
char b;
float c;
};
void main()
{
struct size1 *sptr=0; //declared one pointer to struct and initialise it to zero//
sptr++;
printf("size:%d\n",*sptr);
getch();
}