strtok return value taking bad value after calling malloc <p>I'm making a code that retrieve each every 3 lines of a .csv file and put the values of those lines in a double array (i use a double array), but in the middle of the loop (k=5) i have an issue with the value of "token" which become empty just after i do a malloc on plaintexts[k].</p> <p>I understand that the problem comes from the call of malloc but i don't know how to fix it.</p> <pre><code>int main(){ unsigned char **plaintexts=malloc(sizeof(unsigned char)*30); int i,j,k; FILE *fp; unsigned char *token; unsigned char *str=malloc(sizeof(char)*600); const char s[2] = ","; fp = fopen("aes_traces.csv","r"); double temp; i=0; k=0; while(fgets(str,600,fp)!=NULL){ if(i==0 || i%3==0){ plaintexts[k]=malloc(sizeof(unsigned char)*60); puts(str); token = strtok(str,s); j=0; do{ temp = strtod(token,NULL); plaintexts[k][j] = (unsigned char) (temp); printf("plaintexts[%d][%d] = %d\n",k,j,plaintexts[k][j]); token=strtok(NULL,s); j++; }while(token != NULL); k++; } i++; return 0; } </code></pre>