如何从另一个类似的typedef结构中记忆结构

时间:2014-02-15 10:44:17

标签: c struct typedef memcpy

  1. struct DISPLAY_INFO * display_info;

  2. typedef struct DISPLAY_INFO;

    DISPLAY_INFO display_info_2;

  3. 必须将diplay_info_2复制到display_info。我怎么能用memcpy来做呢。

2 个答案:

答案 0 :(得分:1)

Iff(当且仅当) display_info指向有效的内存区域,您可以写

memcpy(display_info, &display_info_2, sizeof(DISPLAY_INFO));

e.g。

#include <stdio.h>

typedef struct DISPLAY_INFO{
    int val;
} DISPLAY_INFO;

int main() {

    DISPLAY_INFO *display_info;

    display_info = (DISPLAY_INFO*)malloc(sizeof(DISPLAY_INFO));

    DISPLAY_INFO display_info_2; // The object to be copied
    display_info_2.val = 42;

    memcpy(display_info, &display_info_2, sizeof(DISPLAY_INFO));

    printf("%d", display_info->val);

    free(display_info);

    return 0;
}

http://ideone.com/AUwDNw

答案 1 :(得分:0)

你可以这样做:*display_info = display_info_2