In server.c
in pset6, there is a line of code where a string is "subtracted" from a string. I tried to find out what this operation does by writing a simple program :
char* needle = "needle";
char* haystack = "haystack";
int num = haystack - needle;
printf("%d\n",num);
The program printed out the number 7
which after several tries with different strings, is apparently the number of chars in the string needle
including \0
. For some reason changing the string haystack
doesn't affect the result. And replacing the minus operator -
with the plus operator +
results in an error during compilation. Why is this so? How does this operation work?