• C++ retrieve array via returned array pointer .Retrieve array via returned pointer one by one.


    #include <iostream>
    #include <uuid/uuid.h>
    #include <pthread.h>
    
    using namespace std;
    
    void retrieveUuid(char *uuidValue);
    struct BookStruct
    {
        int BookId;
        char *BookName;
        char *BookTitle;
    };
    
    static int num=1;
    
    struct BookStruct * retrieveArr();
    void retrieveStructArr();
    
    int main()
    {
        retrieveStructArr();
        return 0;
    }
    
    void retrieveStructArr()
    { 
        struct BookStruct *p;
        p=retrieveArr();
        for(int i=0;i<100;i++)
        {
            printf("Index=%d,Id=%d,Name=%s,Title=%s\n",i,(p+i)->BookId,(p+i)->BookName,(p+i)->BookTitle);
            free((p+i)->BookName);
            free((p+i)->BookTitle);
        } 
        printf("Finished in retrieveStructArr()!\n");    
    }
    
    struct BookStruct * retrieveArr()
    {
        static struct BookStruct arr[100];
        for(int i=0;i<100;i++)
        {
             arr[i].BookId=num*num*num*num;
             ++num;
             arr[i].BookName=(char*)malloc(40);
             retrieveUuid(arr[i].BookName);
             arr[i].BookTitle=(char*)malloc(40);
             retrieveUuid(arr[i].BookTitle);
        }
        struct BookStruct *structP;
        structP=&arr[0];
        return structP;
    }
    
    void retrieveUuid(char *uuidValue)
    {
        uuid_t newUUID;
        uuid_generate(newUUID);
        uuid_unparse(newUUID, uuidValue);
    }

    Compile as below command

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

    #include <iostream>
    #include <uuid/uuid.h>
    #include <pthread.h>
    
    using namespace std;
    
    void retrieveUuid(char *uuidValue);
    
    struct BookStruct
    {
        int BookId;
        char *BookName;
        char *BookTitle;
    };
    
    static int num=1;
    
    void retrivedSingle(struct BookStruct *structP);
    void array6();
    
    int main()
    {
        array6();
        return 0;
    }
    
    void array6()
    {
        struct BookStruct arr[100];
        for(int i=0;i<100;i++)
        {
            retrivedSingle(&arr[i]);
        }
    
        for(int i=0;i<100;i++)
        {
            printf("Index=%d,Id=%d,Name=%s,Title=%s\n",i,arr[i].BookId,arr[i].BookName,arr[i].BookTitle);
            free(arr[i].BookName);
            free(arr[i].BookTitle);
        }
    
        printf("\nFinished in array6()\n");
    }
    
    void retrivedSingle(struct BookStruct *structP)
    {
        (*structP).BookId=num*num*num*num;
        ++num;
        (*structP).BookName=(char*)malloc(40);
        (*structP).BookTitle=(char*)malloc(40);
        retrieveUuid((*structP).BookName);
        retrieveUuid((*structP).BookTitle);
    }
    
    
    void retrieveUuid(char *uuidValue)
    {
        uuid_t newUUID;
        uuid_generate(newUUID);
        uuid_unparse(newUUID, uuidValue);
    }

    Compile as below:

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

  • 相关阅读:
    Visual C++ 打印编程技术-内存设备环境
    MySQL存储引擎
    记录阿里云服务器docker安装wordpress
    记录dockerfile参数
    记录一次 在公网使用FRP内网穿透开源软件,通过SSH连接内网服务器
    记录一次docker安装zabbix5.0
    记录一次zabbix邮件告警搭建过程和问题处理
    记录一次yum-config-manager命令的使用
    记录一次解决zabbix5.0图形化界面文字乱码的问题
    记录一次查看本地端口10050被哪个IP地址访问
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15683300.html
Copyright © 2020-2023  润新知