I am trying to remove the last comma from a record. I use strrchr() to find the last occurrence of ',' in the record and set it to the null termination. For some reason it is not finding the last occurrence of the comma and giving a "segmentation fault 11" error.
void buildAssemblyRecord(char asmRecord[], const char* data)
{
char* record = asmRecord;
record += sprintf(record, "dc.b\t");
int i = 0;
for(i = 0; i < strlen(data); i++)
{
record += sprintf(record, "$%.2X, ", data[i]);
}
//Remove trailing comma
char* whereComma = strrchr(record, ',');
if(whereComma != NULL)
{
*whereComma = '\0';
}
}
Theoretically this should work perfectly, as I use this method all the time with regular old strchr to remove new line characters from fgets input.
Could anyone let me know whats going on?