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;
    }

  • 相关阅读:
    python中的keys、values、items
    python中的del
    python中的reverse
    python中的remove
    python中的pop
    zookeeper for windows
    express
    js undefine,null 和NaN
    Think_php入口文件配置
    jquery 集合操作
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/15430454.html
Copyright © 2011-2022 走看看