zoukankan      html  css  js  c++  java
  • C++ get struct array via pointer and struct has char * properties

    #include <iostream>
    #include <uuid/uuid.h>
    #include <ctime>
    #include <string.h>
    
    using namespace std;
    
    static char* dtValue=(char*)malloc(20);
    static char* uuidValue=(char*)malloc(40);
    char *getUuidValue1();
    struct BookStruct
    {
        unsigned long long BookId;
        char * BookName;
        char * BookTitle;
    };
    
    void getStructArray(struct BookStruct *ptr,int len);
    void structArray3();
    
    int main()
    {
        structArray3();
        return 0;
    }
    
    void structArray3()
    {
        int len=10000;
        struct BookStruct arr[len];
        getStructArray(arr,len);
        for(int i=0;i<len;i++)
        {
            cout<<"Index="<<i+1<<",Id="<<arr[i].BookId<<",Name="<<arr[i].BookName<<",Title="<<arr[i].BookTitle<<endl;
            free(arr[i].BookName);
            free(arr[i].BookTitle);
        }
        cout<<"Finished in structArray3()"<<endl;
        free(uuidValue);
    }
    
    void getStructArray(struct BookStruct *ptr,int len)
    {
        for(int i=0;i<len;i++)
        {
            ptr->BookId=(unsigned long long)i*i*i*i;
            ptr->BookName=(char*)malloc(40);
            ptr->BookTitle=(char*)malloc(40);
            strcpy(ptr->BookName,getUuidValue1());
            strcpy(ptr->BookTitle,getUuidValue1());
            ptr++;
        }
    }
    
    char *getUuidValue1()
    {
        uuid_t newUUID;
        uuid_generate(newUUID);
        uuid_unparse(newUUID,uuidValue);
        return uuidValue;
    }

    Compile

    g++ -g -std=c++2a -I. h1.cpp -o h1 -luuid

    Run ./h1

  • 相关阅读:
    php RSA公钥私钥加解密和验证用法
    php格式化RSA公钥私钥字符串
    你的周末时光是什么样的?
    php-redis 消息订阅笔记
    php redis 常用操作
    MySql索引笔记
    yii1框架,事务使用方法
    python 项目打包成exe可执行程序
    Linux修改默认端口
    C++字符串作为参数的传递
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15786836.html
Copyright © 2011-2022 走看看