zoukankan      html  css  js  c++  java
  • c struct with char array property

    #include <iostream>
    #include <uuid/uuid.h>
    #include <ctime>
    #include <unistd.h>
    #include <string.h>
    
    using namespace std;
    
    static char *uuidValue=(char*)malloc(40);
    static char *dtValue=(char*)malloc(20);
    
    char *getTimeNow1();
    char *getUuid3();
    struct BookStruct 
    {
        int BookId;
        char BookName[40];
        char BookTitle[40];
    };
    
    void printStructArray6();
    void getBookStructViaPointer(struct BookStruct *bsP,int i);
    
    int main()
    {
        printStructArray6();
        return 0;
    }
     
    void printStructArray6()
    {
        struct BookStruct arr[100];
        for(int i=0;i<100;i++)
        {  
            getBookStructViaPointer(&arr[i],i);
    
            cout<<"Index="<<i<<",Id="<<arr[i].BookId<<",Name="<<arr[i].BookName<<",Title="<<arr[i].BookTitle<<endl;         
        }
    
        for(int i=0;i<100;i++)
        {
            cout<<"Index="<<i<<",Id="<<arr[i].BookId<<",Name="<<arr[i].BookName<<",Title="<<arr[i].BookTitle<<endl;         
        }
        cout<<"Finished in printStructArray6() and now is "<<getTimeNow1()<<endl;
        free(dtValue);
        free(uuidValue);
    }
    
    void getBookStructViaPointer(struct BookStruct *bsP,int i)
    {
        bsP->BookId=i*i*i; 
        strcpy(bsP->BookName,getUuid3());
        strcpy(bsP->BookTitle,getUuid3());
    } 
    
    char *getUuid3()
    {
        uuid_t newUUID;
        uuid_generate(newUUID);
        uuid_unparse(newUUID,uuidValue);
        return uuidValue;
    }
    
    char *getTimeNow1()
    {
        time_t rawTime=time(NULL);
        struct tm tmInfo=*localtime(&rawTime);
        strftime(dtValue,20,"%Y%m%d%H%M%S",&tmInfo);
        return dtValue;
    }
    g++ -g -std=c++2a -I. h1.cpp -o h1 -luuid

    Run ./h1

    Another way is to get array in a batch via pointer and increment pointers

    void getBookStructArray8(struct BookStruct *ptr,int len)
    {
        for(int i=0;i<100;i++)
        {
             ptr->BookId=i*i*i*i;
             strcpy(ptr->BookName,getUuid3());
             strcpy(ptr->BookTitle,getUuid3());
             ptr++;
        }   
    }
     
    void printStructArray6()
    {
        int len=100;
        struct BookStruct arr[len]; 
        getBookStructArray8(arr,len);  
        for(int i=0;i<len;i++)
        {
            cout<<"Index="<<i<<",Id="<<arr[i].BookId<<",Name="<<arr[i].BookName<<",Title="<<arr[i].BookTitle<<endl;         
        }
        cout<<"Finished in printStructArray6() and now is "<<getTimeNow1()<<endl;
        free(dtValue);
        free(uuidValue);
    }
  • 相关阅读:
    小米手机做USB电脑摄像头啦,亲测可用,附有详细教程!
    【DIY文章列表标签】dt_gry_list
    Oracle 10g 设置 PL/SQL 远程
    关于硬盘“4K扇区”对齐的查看与设置方法
    oracle数据误操作恢复【flashback闪回操作】
    CENTOS下安装LNMP环境随笔
    深喉咙使用心得(陆续更新ing....)
    CENTOS6.3环境下安装VSFTPD 便于开通FTP功能随笔
    MYSQL/SQL_SERVER/ORACLE三种数据库自动备份方法
    U盘安装 ubuntu 12.04随笔
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15766171.html
Copyright © 2011-2022 走看看