I am having problems using this pointer to pointers to chars that represents an array of strings.
I should dereference it with some offset i to get the i-th string, then, dereference the result with some other offset j to get the j-th character of such string.
In my understanding, and assuming the indices are inbound, I could do that in several syntactic ways, so for example, to get the 6th character of the second string:
char* myFun(char** strs, int size){
char a;
int i=2,j=6;
a=strs[i][j];
a=*(strs[i]+j);
a=(*strs+i)[j];
a=*(*(strs+i)+j);
}
However, I am getting a runtime error runtime error: load of null pointer of type 'char'
on the line of the for
loop on the following code, perhaps, the error is not the one that I suspect, but that is the reason I ask you for help.
char * myFn(char ** strs, int strsSize){
if(*strs=NULL || strsSize==0){
printf("empty first string, or empty array of strings\n");
return NULL;
}
int i,pf_s=1;
char * pf_str=(char*)calloc(pf_s,sizeof(char));
for(i=0;*(strs[0]+i)!='\0';++i){// <-------- runtime error
if(!(i<pf_s-2)){
pf_s=pf_s*2;
pf_str=(char*)realloc(pf_str,2*pf_s*sizeof(char));
}
*(pf_str+i)=(*strs)[i];
}
pf_str[i]='\0';
pf_str=(char*)realloc(pf_str,i+1*sizeof(char));
printf("%s\n",pf_str);
return pf_str;
}