How to find the size of structure without using sizeof operator in C

2 Answers

0 votes
#include <stdio.h> 
 
struct  test
{
    int n;
    char ch;
    float f;
};
 
int main(int argc, char **argv) 
{ 
    struct test arr[2];
    
    int size = (char*)&arr[1] - (char*)&arr[0];
    
    printf("%d",size);
    
    return(0);
}

 
 
/*
 
run:
 
12
 
*/

 



answered May 5, 2017 by avibootz
0 votes
#include <stdio.h> 
 
struct  test
{
    int n;
    char ch;
    float f;
};
 
int main(int argc, char **argv) 
{ 
    struct test t;
    struct test *p = &t;

    int size = (char*)(p + 1) - (char*)p;
    
    printf("%d",size);
    
    return(0);
}

 
 
/*
 
run:
 
12
 
*/

 



answered May 5, 2017 by avibootz

Related questions

1 answer 185 views
1 answer 188 views
188 views asked Jan 18, 2019 by avibootz
2 answers 147 views
1 answer 138 views
138 views asked Mar 8, 2024 by avibootz
1 answer 2,793 views
2 answers 177 views
1 answer 128 views
128 views asked Jul 3, 2024 by avibootz
...