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