将数组复制到结构

标签 c arrays memory structure copying

我有一个 9 字节的数组,我想将这些字节复制到一个结构中:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _structure {
    char one[5];        /* 5 bytes */
    unsigned int two;   /* 4 bytes */
} structure;

int main(int argc, char **argv) {

    structure my_structure;

    char array[]    = {
        0x41, 0x42, 0x43, 0x44, 0x00,   /* ABCD\0 */
        0x00, 0xbc, 0x61, 0x4e          /* 12345678 (base 10) */
    };

    memcpy(&my_structure, array, sizeof(my_structure));

    printf("%s\n", my_structure.one);   /* OK, "ABCD" */
    printf("%d\n", my_structure.two);   /* it prints 1128415566 */

    return(0);
}

结构my_structure的第一个元素one被正确复制;但是,my_structure.two 包含 1128415566,而我期望为 12345678。arraymy_structure 具有不同的大小,即使它们的大小相同,仍然存在two 会有问题。我该如何解决这个问题?

最佳答案

有几个问题:

出于效率原因,编译器对齐边界上的变量等于处理器的寄存器大小。 IE。在 32 位系统上,这将在 32 位(4 字节)边界上。 此外,结构将具有“间隙”,以便结构成员可以在 32 位边界上对齐。换句话说:该结构没有被紧密地“打包”。 试试这个:

#include <stdio.h>

typedef struct
{
    char one[5];        /* 5 bytes */
    unsigned int two;   /* 4 bytes */
}
    structure;
structure my_structure;

char array[] = 
{
    0x41, 0x42, 0x43, 0x44, 0x00,   /* ABCD\0 */
    0x00, 0xbc, 0x61, 0x4e          /* 12345678 (base 10) */
};

int main(int argc, char **argv) 
{
    const int sizeStruct = sizeof(structure);
    printf("sizeof(structure) = %d bytes\n", sizeStruct);
    const int sizeArray = sizeof(array);
    printf("sizeof(array) = %d bytes\n", sizeArray);
    return 0;
}

您应该会看到不同的尺寸。

您可以使用#pragma 或属性指令覆盖此行为。 使用 gcc,您可以使用属性更改结构定义。例如。更改上面的代码以添加“打包”属性(需要 gcc):

typedef struct __attribute__((packed))

然后再次运行程序。现在尺寸应该是一样的。 注意:在某些处理器架构上,例如ARMv4,32 位变量必须在 32 位边界上对齐,否则您的程序将无法运行(出现异常)。 阅读“对齐”和“打包”编译指示或属性的编译器文档。

下一个问题是字节顺序。试试这个:

printf("0x%08X\n", 12345678);

12345678 的十六进制是 0x00BC614E。从你的例子和你得到的输出,我可以看出你的平台是“小端”。在“little endian”系统中,数字 0x00BC614E 存储为从最低有效字节开始的字节序列,例如0x4E、0x61、0xBC、0x00。所以改变你的数组定义:

char array[] = 
{
    0x41, 0x42, 0x43, 0x44, 0x00,   /* ABCD\0 */
    0x4E, 0x61, 0xBC, 0x00,         /* 12345678 (base 10) */
};

现在您的程序将打印 12345678。

另请注意,您应该使用 %u 来打印无符号整数。

复制 char 字符串可能是一堆蠕虫,特别是如果您必须允许不同的编码(例如 Unicode)。至少,您需要确保复制目标缓冲区不受溢出影响。

修改后的代码:

#include <stdio.h>
#include <string.h>

typedef struct
{
    char one[5];        /* 5 bytes */
    unsigned int two;   /* 4 bytes */
}
    structure;

structure my_structure;

char array[] = 
{
    0x41, 0x42, 0x43, 0x44, 0x00,   /* ABCD\0 */
    0x4E, 0x61, 0xBC, 0x00,         /* 12345678 (base 10) */
};

int main() 
{
    // copy string as a byte array
    memcpy(&my_structure.one, &array[0], sizeof(my_structure.one));

    // copy uint
    my_structure.two = *((unsigned int *)(&array[5]));

    printf("%s\n", my_structure.one);
    printf("%u\n", my_structure.two);

    return 0;
}

最后,依赖打包数据结构通常不是一个好主意,因为它会使代码移植到不同的平台变得困难。但是,有时您需要打包/解包协议(protocol)数据包。在这些特殊情况下,通常最好和最便携的方法是针对每种数据类型使用一对函数手动打包/解包每个项目。

我将把字节顺序问题留到另一个主题。 :-)

关于将数组复制到结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621836/

相关文章:

arrays - 警告 : format ‘%s’ expects type ‘char *’ , 但参数 2 的类型为 ‘char (*)’

java - Integer[] 与 int[] : analysis using classmexer. jar 的内存使用情况

python - 是什么导致 python 列表的(大)大小?

c++ - 我怎样才能知道 C++ 类的一个实例消耗了多少内存?

c - 从没有 malloc 的函数返回字符串

c - 如何覆盖文本文件中的内容? (编辑项目)

c - 在将数组的递增地址传递给函数时如何获得此输出?

javascript - 数组连接函数不起作用

javascript - 在 localStorage 中存储有序对象/数组

ruby - 如何在ruby中获取字符串数组的总和