How to find the size of a variable without using sizeof

后端 未结 10 1661
终归单人心
终归单人心 2020-12-17 06:07

Let us assume I have declared the variable \'i\' of certain datatype (might be int, char, float or double) ...

NOTE: Simply consider that \'i\' is

相关标签:
10条回答
  • 2020-12-17 06:32

    This should give you the size of your variable

    #define mySizeof(type) ((uint)((type *)0+1))
    
    0 讨论(0)
  • 2020-12-17 06:33

    I hope that below code would solve your problem in c++ without using sizeof() operator

    for any variables like (int, char, float, double, char, short and many more...)

    here I take integer,

    int a;
    

    then show it as byte addressable output,

    cout<<(char *)(&a + 1) - (char *)(&a);
    
    0 讨论(0)
  • 2020-12-17 06:35

    Program to find Size of the variable without using sizeof operator

    #include<stdio.h>
    int main()
      {
      int *p,*q;
      int no;
      p=&no;
       printf("Address at p=%u\n",p);
      q=((&no)+1);
      printf("Address at q=%u\n",q);
      printf("Size of int 'no': %d Bytes\n",(int)q-(int)p);
    
      char *cp,*cq;
        char ch;
      cp=&ch;
      printf("\nAddress at cp=%u\n",cp);
      cq=cp+1;
      printf("Address at cq=%u\n",cq);
      printf("Size of Char=%u Byte\n",(int)cq-(int)cp);
    
      float *fp,*fq;
      float f;
      fp=&f;
     printf("\nAddress at fp=%u\n",fp);
      fq=fp+1;
      printf("Address at fq=%u\n",fq);
      printf("Size of Float=%u Bytes\n",(int)fq-(int)fp);
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-17 06:38
    #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();
    }
    
    0 讨论(0)
提交回复
热议问题