Last active
August 29, 2015 14:18
-
-
Save mihids/3cebb799c8daf0685116 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <string.h> | |
struct Item { | |
int i; | |
char buf1[10]; | |
char buf2[20]; | |
}; | |
int main() | |
{ | |
Item item; | |
std::cout << "Start of struct " << (&item) << std::endl; | |
std::cout << "Start of i in struct " << &(item.i) << std::endl; | |
std::cout << "Start of buf1 in struct " << &(item.buf1) << std::endl; | |
std::cout << "Start of buf2 in struct " << &(item.buf2) << std::endl; | |
int a = 10; | |
char* pBuf1 = new char[20]; | |
strncpy(pBuf1, "test123", 10); | |
char* pBuf2 = new char[20]; | |
strncpy(pBuf2, "123test", 10); | |
memcpy(&item.i, &a, sizeof(a)); | |
memcpy(&item.buf1, pBuf1, sizeof(item.buf1)); | |
//memcpy(&item+4, pBuf1, sizeof(item.buf1)); // doesn't work because +4 works as &item+4*sizeof(item) | |
memcpy(&item.buf2, pBuf2, sizeof(item.buf2)); | |
std::cout << item.i << std::endl; | |
std::cout << item.buf1 << std::endl; | |
std::cout << item.buf2 << std::endl; | |
Item item2; | |
memcpy(&item2, &item, sizeof(Item)); | |
std::cout << item2.i << std::endl; | |
std::cout << item2.buf1 << std::endl; | |
std::cout << item2.buf2 << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment