如何在不使用sizeof的情况下找到变量的大小

问题描述 投票:3回答:10

让我们假设我已经声明了某些数据类型的变量'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就不用理会或...

c sizeof
10个回答
22
投票

您可以使用以下宏,取自here


-2
投票

下面的语句将给出通用的解决方案:


7
投票

自从我编写任何C代码以来已经很久了,我从来都不擅长,但是看起来不错:


3
投票

这有效..


2
投票
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);

0
投票

我希望下面的代码能在不使用sizeof()运算符的情况下解决c ++中的问题


-1
投票

尝试一下,


-1
投票

这应该给您变量的大小


-1
投票

无需使用sizeof运算符即可查找变量大小的程序


-2
投票
#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();
}
© www.soinside.com 2019 - 2024. All rights reserved.