- #1
Jamin2112
- 986
- 12
I have a chunk of code that is like
and I want to do it without the strlen(dgdx) and strlen(dhdx) because they seem redundant considering that the implementation of strcat iterates over all the characters of the string again. How do I redo this overall crappy procedure?
Code:
if (rt->op) // if rt is of the form rt = gx op hx
{
char * dgdx = deriveFromTree(rt->gx); // g'(x)
char * dhdx = deriveFromTree(rt->hx); // h'(x)
char thisop = *rt->op;
if (thisop == '+' || thisop == '-')
{
// ADDITION/SUBTRACTION RULE:
// dfdx = dgdx + thisop + dhdx
long n = strlen(dgdx) + strlen(dhdx) + 2;
dfdx = malloc(sizeof(char) * n); dfdx[n-1]='\0';
dfdx = strcat(dfdx, dgdx);
dfdx = strcat(dfdx, charToString(thisop));
dfdx = strcat(dfdx, dhdx);
}
and I want to do it without the strlen(dgdx) and strlen(dhdx) because they seem redundant considering that the implementation of strcat iterates over all the characters of the string again. How do I redo this overall crappy procedure?