I am trying to come up with my own code for strcat. This is what I have so far. I'm getting a segmentation fault and i can't figure out why. The function prototype and call must not change. Any suggestions?

//Function Protoype
char* stringCat(char *pWord, char *pTemp);

//Function call in main
 stringCat(stringCat(word, ": "), temp);
   printf("\n\nConcatenation: \"%s\".", word);
   printf("\n\n");

//Actual Function
char* stringCat(char *pWord, char *pTemp)
{ 
   int i;   
   int j;
   
   for (i = 0; pWord[i] != '\0'; i++)
      for (j = 0; pTemp[j] != '\0'; j++)
         pWord[i+j] = pTemp[j];
   pWord[i+j] = '\0';
   
   return pWord;
}
//Function Protoype
char* stringCat(char *pWord, char *pTemp);

//Function call in main
 stringCat(stringCat(word, ": "), temp);
   printf("\n\nConcatenation: \"%s\".", word);
   printf("\n\n");

//Actual Function
char* stringCat(char *pWord, char *pTemp)
{ 
   int i;   
   int j;
   
   for (i = 0; pWord[i] != '\0'; i++);    // Notice the ;
   for (j = 0; pTemp[j] != '\0'; j++)
      pWord[i+j] = pTemp[j];
   pWord[i+j] = '\0';
   
   return pWord;
}

Or to avoid costly addition operations

for (j = 0; pTemp[j] != '\0'; j++,i++)
      pWord[i] = pTemp[j];
   pWord[i] = '\0';
commented: useful info! +1

Or to avoid costly addition operations

for (j = 0; pTemp[j] != '\0'; j++,i++)
      pWord[i] = pTemp[j];
   pWord[i] = '\0';

@ Ancient Dragon
Sir, please clear "to avoid costly addition operations"....

I am regularly following your posts and its helping me alot to improve my knowledge.:)

In the loop i+j requires the addition of i and j in order to resolve the index into pWord. That takes unnecessary clock cycles. Just simply continue to use the i counter that already contains the value that is needed in the loop.

In the loop i+j requires the addition of i and j in order to resolve the index into pWord. That takes unnecessary clock cycles. Just simply continue to use the i counter that already contains the value that is needed in the loop.

Thanks for clearing me!!

Thanks for the help. It worked. The little ; is an evasive little bugger. Thanks again.

Or to avoid costly addition operations

for (j = 0; pTemp[j] != '\0'; j++,i++)
      pWord[i] = pTemp[j];
   pWord[i] = '\0';

And to clarify, if you do this you should initialize i as 0 or it will hold random numbers.

>>And to clarify, if you do this you should initialize i as 0 or it will hold random numbers.
No you don't want to initialize the value of i to anything because it will contain the correct value from the previous loop.

>>And to clarify, if you do this you should initialize i as 0 or it will hold random numbers.
No you don't want to initialize the value of i to anything because it will contain the correct value from the previous loop.

Sorry then, I missread your post I thought it would be just one loop. (j)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.