I m reallocating a char**
array with every entry and while compiling comes back clean, only the first entry is stored and I get Segmentation fault (core dumped) always when i try to register a 4th entry.
Here is the relevant code in main.c:
int main(int argc, char *argv[])
{
int i,sizea,sizeb,choice,letters,check,mistakes,count;
char C[26][2];
char **A,**B,a;
A=(char**)malloc(sizeof(char*));
*A=(char*)malloc((MAX_CHAR+1)*sizeof(char));
sizea=1;
build(&A,&sizea);
return 0;
}
And here is the implementation of the method in mylib.c:
void build(char ***A, int *sizea)
{
*A=(char**)realloc(*A,(*sizea) * sizeof(char*));
*A[*sizea-1]=(char*)malloc((MAX_CHAR+1)*sizeof(char));
printf("Give word :");
scanf("%s",(*A[*sizea-1]));
(*sizea)++;
}
Thanks a lot for your help.
edit: similar problems in this method that werent fixed by doing the same thing
void find(char **A, char ***B, int letters,int sizea, int *sizeb){
int i,j,k,dummy;
char a='a';
for(i=0;i<(sizea-1);i++){
printf("here\n");
if(A[i][letters]=='\0'){
*B=(char**)realloc(*B,(*sizeb+1) * sizeof(char*));
(*B)[*sizeb]=(char*)malloc((letters+1)*sizeof(char));
(*B)[*sizeb-1]=A[i];
*sizeb++;
printf("%s\n", (*B)[i]);
}
}
}
The problem is here:
scanf("%s",(*A[*sizea-1]));
The array index operator []
has higher precedence than the dereference operator *
. So the above parses as:
scanf("%s",(*(A[*sizea-1])));
What you want is:
scanf("%s",((*A)[*sizea-1]));
Similarly, this:
*A[*sizea-1]=(char*)malloc((MAX_CHAR+1)*sizeof(char));
Should be:
(*A)[*sizea-1]=(char*)malloc((MAX_CHAR+1)*sizeof(char));