How to get the size in bytes of a member of a struct in C

3 Answers

0 votes
#include <stdio.h> 

struct employee
{
    int id;
    char name[32];
};

  
int main(void)
{   
    size_t size_in_byte = sizeof(((struct employee *) 0)->id);
    printf("%zu\n", size_in_byte);
    
    size_in_byte = sizeof(((struct employee *) 0)->name);
    printf("%zu\n", size_in_byte);
    
    return 0;
}


        
/*
run:
     
4
32
    
*/

 



answered Jul 3, 2024 by avibootz
0 votes
#include <stdio.h> 

typedef struct 
{
    int id;
    char name[32];
} employee;

  
int main(void)
{   
    size_t size_in_byte = sizeof(((employee *) 0)->id);
    printf("%zu\n", size_in_byte);
    
    size_in_byte = sizeof(((employee *) 0)->name);
    printf("%zu\n", size_in_byte);
    
    return 0;
}


        
/*
run:
     
4
32
    
*/

 



answered Jul 3, 2024 by avibootz
0 votes
#include <stdio.h> 
 
typedef struct
{
    int id;
    char name[32];
} employee;
 
   
int main(void)
{   
    employee e;
    
    size_t size_in_byte = sizeof(e.id);
    printf("%zu\n", size_in_byte);
     
    size_in_byte = sizeof(e.name);
    printf("%zu\n", size_in_byte);
     
    return 0;
}
 
 
         
/*
run:
      
4
32
     
*/

 



answered Jul 3, 2024 by avibootz

Related questions

2 answers 132 views
1 answer 151 views
1 answer 213 views
213 views asked Dec 26, 2020 by avibootz
1 answer 255 views
2 answers 167 views
167 views asked Apr 28, 2022 by avibootz
...