How to determine 2D unsigned short pointers array length in c++
- by tuman
Hello,
I am finding it difficult to determine the length of the columns in a 2D unsigned short pointer array. I have done memory allocation correctly as far as I know. and can print them correctly.
plz see the following code segment:
int number_of_array_index_required_for_pointer_abc=3;
char A[3][16];
strcpy(A[0],"Hello");
strcpy(A[1],"World");
strcpy(A[2],"Tumanicko");
cout<<number_of_array_index_required_for_pointer_abc*sizeof(unsigned short)<<endl;
unsigned short ** pqr=(unsigned short **)malloc(number_of_array_index_required_for_pointer_abc*sizeof(unsigned short));
    for(int i=0;i<number_of_array_index_required_for_pointer_abc;i++)
    {
        int ajira = strlen(A[i])*sizeof(unsigned short);
        cout<<i<<" = "<<ajira<<endl;
        pqr[i]=(unsigned short *)malloc(ajira);
        cout<<"alocated pqr[i]= "<<sizeof pqr<<endl;
        int j=0;
        for(j=0;j<strlen(A[i]);j++)
        {
            pqr[i][j]=(unsigned short)A[i][j];
        }
        pqr[i][j]='\0';
    }
    for(int i=0;i<number_of_array_index_required_for_pointer_abc;i++)
    {
        //ln= (sizeof pqr[i])/(sizeof pqr[0]);
        //cout<<"Size of pqr["<<i<<"]= "<<ln<<endl;
         // I want to know the size of the columns i.e. pqr[i]'s length instead of finding '\0'
         for(int k=0;(char)pqr[i][k]!='\0';k++)
        cout<<(char)pqr[i][k];
        cout<<endl;
    }