我定义了以下结构:

typedef struct {
    int num_albaran;
    char cod_vendedor[5];
    char cod_cliente[5];
    char cod_articulo[5];
    int unidades_vendidas;
    char fecha_venta[5];
    float desc_aplicado;
    float total_venta;
} plantilla_venta;

typedef struct {
    long int num_registros;
    char blancos[36]; // tengo que meterle esa cantidad porque la estructura de ventas me da 40 a saber por que....
} plantilla_primer_registro;


当我使用sizeof函数时:

printf("%d - %d", sizeof(plantilla_venta), sizeof(plantilla_primer_registro));


我得到以下尺寸:

40 - 40


这是不正确的,这意味着我在向文件读取或写入数据时遇到了麻烦。我该如何纠正?

最佳答案

正确,因为此结构应为40个字节。
因为:

you have 2 integers = 2 * 2 bytes = 4 bytes. In some systems int is 2 bytes

4 char array of size 5 = 4 * 5 bytes = 20 bytes. An ascii char is 1 byte ( Unicode chars take 2 bytes )

2 float numbers = 2 * 8 bytes = 16 bytes.

如果将它们总计为8+20+16 bytes,则结果为40 bytes,那是什么问题呢?

关于c - CodeBlock中的结构大小错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34861603/

10-12 00:20
查看更多