zoukankan      html  css  js  c++  java
  • C++拷贝内存的方式给属性赋值

    #include <iostream>
    #include <string.h>


    using namespace std;

    struct TestStruct {
        int id;
        char *a{nullptr};
        char *b{nullptr};
        char *c{nullptr};
    };
    int main(int argc, const char *argv[]) {
        int size = sizeof(TestStruct) + 6 + 3 + 2;
       auto st = (TestStruct *) malloc(size);
        st->id = 100;

        char *c = "qiumc";
        char *d = reinterpret_cast<char *>(st + sizeof(TestStruct));
        memcpy_s(d, 6, c, 6);
        st->a = d;

        c = "11";
        d = reinterpret_cast<char *>(st + sizeof(TestStruct) + 6);
        memcpy_s(d, 3, c, 3);
        st->b = d;

        c = "x";
        d = reinterpret_cast<char *>(st + sizeof(TestStruct) + 6 + 3);
        memcpy_s(d, 2, c, 2);
        st->c= d;

        cout << st->a << endl;
        cout << st->b << endl;
        cout << st->c << endl;
    }

  • 相关阅读:
    《将博客搬至CSDN》
    所谓找链表中点
    虚函数
    编辑距离
    数组移位
    DFA
    Single Number III
    XOR异或解惑
    First Bad Version
    while(!in.empty()) 和 while(in.size())
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/15430454.html
Copyright © 2011-2022 走看看