2014-02-15 107 views
0
  1. struct DISPLAY_INFO * display_info;如何從另一個類似類型結構memcpy結構

  2. typedef struct DISPLAY_INFO;

    DISPLAY_INFO display_info_2;

必須將diplay_info_2複製到display_info。我怎樣才能使用memcpy來做到這一點。

+0

你有什麼問題使用'memcpy'? –

+0

display_info是**指針**結構,它是否指向有效的內存區域? –

回答

1

IFF(當且僅當)display_info指向一個有效的存儲區,你可以寫

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

例如

#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

0

你可以這樣做:*display_info = display_info_2