C++retrieve array via returned pointer,traverse the array pointer without the array size

char **arrayP6();
void retrieveUuid(char *uuidValue);
void array8();

int main()
{
    array8();
    return 0;
}

void array8()
{
    char **p=arrayP6();
    for(int i=0;i<100;i++)
    {
        printf("Index=%d,value=%s in array8()\n",i,*(p+i));
    }
}

char **arrayP6()
{
    static char *arr[100]; 
    for(int i=0;i<100;i++)
    {
        arr[i]=(char*)malloc(40);
        retrieveUuid(arr[i]);
    }
    char **p=arr;
    return p;
}

 

 

 

 

Part II

When I try to traverse the array via pointer, but I don't know its size exactly. So I use the while loop and pointer self-increment method.

char **arrayP6();
void retrieveUuid(char *uuidValue);
void array8();

int main()
{
    array8();
    return 0;
}

void array8()
{
    char **p=arrayP6();
    int num=0;
    while(*p)
    {
        printf("Index=%d,value=%s in array8()\n",++num,*p);
        *p++;
    } 
}

char **arrayP6()
{
    static char *arr[100]; 
    for(int i=0;i<100;i++)
    {
        arr[i]=(char*)malloc(40);
        retrieveUuid(arr[i]);
    }
    char **p=arr;
    return p;
}

void retrieveUuid(char *uuidValue)
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID,uuidValue);
}

 

posted @ 2021-12-06 09:59  FredGrit  阅读(25)  评论(0编辑  收藏  举报