How to set struct to exact site of its bytes without memory padding in C

1 Answer

0 votes
#include <stdio.h>

typedef struct Info1 {
    int n; // 4
    short st; // 2
    char ch; // 1
    char s[5]; // 5
    int *p; // 8
} Info1;


#pragma pack(1)
typedef struct Info2 {
    int n; // 4
    short st; // 2
    char ch; // 1
    char s[5]; // 5
    int *p; // 8
} Info2;

int main(void) {
    int a;
    Info1 inf1 = {812, 17, 'n', "c c++", &a};
    Info2 inf2 = {812, 17, 'n', "c c++", &a};
    
    printf("Size: %llu\n", sizeof(inf1));
    printf("Size: %llu\n", sizeof(inf2));
    
    return 0;
}



/*
run:

Size: 24
Size: 20

*/

 



answered Dec 26, 2020 by avibootz

Related questions

1 answer 194 views
194 views asked Dec 26, 2020 by avibootz
1 answer 147 views
147 views asked May 4, 2021 by avibootz
1 answer 211 views
2 answers 273 views
1 answer 176 views
176 views asked Jul 10, 2015 by avibootz
...