c struct struct array,pointer,uuid,memcpy
#include <stdio.h> #include <stdlib.h> #include <uuid/uuid.h> #include <string.h> void retrieveUuid(char *uuidValue) { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID, uuidValue); } struct BookStruct { int BookId; char *BookAuthor; char *BookISBN; }; struct BookStruct *arrayPointer4(); void structPointer5(); int main() { structPointer5(); } void structPointer5() { struct BookStruct *bsp; bsp=arrayPointer4(); for(int i=0;i<100;i++) { printf("Id=%d,Author=%s,ISBN=%s\n",(bsp+i)->BookId,(bsp+i)->BookAuthor,(bsp+i)->BookISBN); } } struct BookStruct *arrayPointer4() { static struct BookStruct arr[100]; char *msg = (char *)malloc(40); for (int i = 0; i < 100; i++) { struct BookStruct bs; bs.BookAuthor = (char *)malloc(40); bs.BookISBN = (char *)malloc(40); retrieveUuid(msg); memcpy(bs.BookAuthor,msg,40); retrieveUuid(msg); memcpy(bs.BookISBN,msg,40); bs. BookId=i*i*i*i; arr[i]=bs; } free(msg); struct BookStruct*bsp=&arr; return bsp; }
void charArr8() { char **p=charArray7(); for(int i=0;i<100;i++) { printf("I=%d,value=%s\n",i,*(p+i)); free(*(p+i)); } } char **charArray7() { static char * arr[100]; char *msg=(char*)malloc(40); for(int i=0;i<100;i++) { arr[i]=(char*)malloc(40); retrieveUuid(msg); memcpy(arr[i],msg,40); } free(msg); char **p; p=&arr; return p; }
struct BookStruct* structP10(); void structP11(); int main() { structP11(); } void structP11() { struct BookStruct *p=structP10(); for(int i=0;i<100;i++) { printf("I=%d,Author=%s,ISBN=%s\n",(p+i)->BookId,(p+i)->BookAuthor,(p+i)->BookISBN); free((p+i)->BookAuthor); free((p+i)->BookISBN); } } struct BookStruct* structP10() { static struct BookStruct arr[100]; char *msg=(char*)malloc(40); for(int i=0;i<100;i++) { struct BookStruct bs; bs.BookId=i*i*i*i; bs.BookAuthor=(char*)malloc(40); bs.BookISBN=(char*)malloc(40); retrieveUuid(msg); memcpy(bs.BookAuthor,msg,40); retrieveUuid(msg); memcpy(bs.BookISBN,msg,40); arr[i]=bs; } struct BookStruct*p; p=arr; free(msg); return p; }