-
Success
Hey guys I woke up early this morning and finished this code. Check out the working changes, and thanks to ItCbitC and MK27 for getting me over this hump. I owe you two my life!
p.s. figuring out that non duplicate algo was a pain in the rear
Code:
dupArray = (char**)malloc(val*sizeof(char*));
fseek(finp_dat, seek_val, SEEK_SET);
while((fread(&eof_test, sizeof(char), 1, finp_dat))!= NULL)
{
fseek(finp_dat, -1, SEEK_CUR);
fread(test_string, sizeof(char),len, finp_dat);
dupArray[j] = strdup(test_string);
j++;
fseek(finp_dat, advance_val, SEEK_CUR);
}
for(i=0;i<5;i++)
{
for(k=i;k<5;k++)
{
if((i!= k) && ((strcmp(dupArray[i], dupArray[k])==0)))
{
dupArray[k] = "dup";
//printf("%s\n", dupArray[k]);
}
}
}
for(i=0;i<5;i++)
{
if(dupArray[i]!= "dup")
printf("%s\n", dupArray[i]);
}
for(i=0;i<val;i++)
{
free(dupArray[i]);
}
free(dupArray);
}
-
Quote:
dupArray[k] = "dup";
when you will free this pointer - your program will crash
-
I agree with vart, dupArray[k] now points to a string literal so trying to free() a pointer that has not been allocated dynamically is undefined behaviour. IMHO a better design would be to free(dupArray[k]) and then set it to NULL. Doing it this way, conditionally print only those elements that are not NULL.