-2

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;
}
onlycparra
  • 607
  • 4
  • 22
  • As you've solved your problem and it's a typo you should delete this question. – Fiddling Bits Feb 17 '20 at 21:34
  • 2
    Is this question that useless? beyond whether it is a typo or not, I think that somebody with the same error message could land here and see that they may as well be de-referencing a null pointer (perhaps not so evidently as in my code). – onlycparra Feb 21 '20 at 04:59

1 Answers1

0

In the second line of code, NULL is being assigned to *strs, instead of comparing *strs against NULL.

Change this:

if(*strs=NULL || strsSize==0){

Into this:

if(*strs==NULL || strsSize==0){
onlycparra
  • 607
  • 4
  • 22