1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

構造体同士のコピー

Posted at

x 過去ログを見ょ!!!
x M5NanoC6での予定は未定

目的
構造体と構造体をコピーする
セグメントエラー防止の為に
短い方に合わせる

いろいろ
マクロ化したほうが、たぶんスッキリする

オンラインコンパイラ paiza



#include <iostream>
#include <string.h>  //memcpy
using namespace std;
int main(void){
    // Your code here!
    
    struct {
        float x = 1.0f;
        float y = 2.0f;
    } X_Y;
    
    struct {
        float x;
        float y;
    } buff;
   
    
    
    unsigned char *p;
    p = (unsigned char *)(&X_Y);
    printf("X_Y\n");
    printf("[%x][%x][%x][%x]\n",p[0],p[1],p[2],p[3]);
    printf("[%x][%x][%x][%x]\n",p[4],p[5],p[6],p[7]);


    memcpy(&buff, &X_Y,  (  sizeof(buff) < sizeof(X_Y)  ) ? sizeof(buff) : sizeof(X_Y)  ); 
    
    
    p = (unsigned char *)(&buff);
    printf("buff\n");
    printf("[%x][%x][%x][%x]\n",p[0],p[1],p[2],p[3]);
    printf("[%x][%x][%x][%x]\n",p[4],p[5],p[6],p[7]);
    
    printf("\n");
    printf("[%f],[%f]\n", buff.x, buff.y);
    
}




X_Y
[0][0][80][3f]
[0][0][0][40]
buff
[0][0][80][3f]
[0][0][0][40]

[1.000000],[2.000000]



1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?