In general I know why you get this error, but I'm a little confused in this particular instance...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* mystrncpy(char* dst, const char* src, size_t n) {
char* temp = dst;
while (n-- > 0 && (*temp = *src)) {
temp++;
src++;
}
return dst;
}
int main() {
const char* str = "Hello World!";
char buf[50];
memset(buf, 0, sizeof(buf));
mystrncpy(buf, str, sizeof(buf));
printf("%s\n", buf);
}
The code above works great, but if I remove the extra set of brackets changing the while loop to:
while (n-- > 0 && *temp = *src)
Then I get the error. I guess it's something about operator precedence but I'm a little baffled. Can someone explain what that while loop alteration does to make this compiler error appear?