I am trying to understand postfix & prefix increment and decrement operators from K&R book. They have given a string concatenation example:
Code:
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char s[], char t[])
{
int i, j;
i = j = 0;
while (s[i] != '\0') /* find end of s */
i++;
while ((s[i++] = t[j++]) != '\0') /* copy t */
;
}
I've used the above function in my code but to mess around (and to understand concept) I've switched the opertors:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 50
void trcat (char source[], char target[]);
int main(void) {
char target[MAXLINE] = "test";
char source[MAXLINE] = "this is ";
trcat(source, target);
printf("%s", source);
}
void trcat(char s[], char t[])
{
int i, j;
i = j = 0;
while (s[i] != '\0') /* find end of s */
i++;
while ((s[++i] = t[++j]) != '\0') /* copy t */
;
}
Obviously I am not getting desired output but I am unable to pinpoint the problem.
In the above example concatenation starts to happen when i=8 but to mess around (++i) I prefix the offset so in my case i becomes 9 before assignment. Same is the case with j. So as per my understanding & at best program should have skipped a few characters but in my case the concatenation is not happening.
i.e. the output is just:
I am confused. Kindly help.