This is a pointer value:
tempstr
This is another pointer value (which points to 5
elements beyond the tempstr
pointer value):
tempstr + 5
This is an integer:
strlen(line)
Therefore, this is a pointer value (which points to strlen(line)
elements beyond the tempstr
pointer value):
tempstr + strlen(line)
And this is dereferencing that pointer:
*(tempstr + strlen(line))
This is what I think, correct if I am wrong. *(tempstr + strlen(line)) is adding the length of line to tempstr and dereferencing and assigning the 0x0 converted to a char?
It's ensuring that the character at index 20 of tempstr, immediately beyond the "AUTO_ANSWER_CALL = 1" characters, is null: i.e. it's ensuring that the string is null-terminated.
That string already is null-terminated, by the way (so that last statement is redundent): because strcpy copies the string including the implicit null-termination character.
Isn't easier just to do the following tempstr[sizeof(tempstr)-1] = '\0'; this is much easier to understand.
These aren't the same thing: strlen(line)
equals 20, but sizeof(tempstr)
equals 128.
would this work: tempstr[strlen(tempstr)] = '\0'
That's exactly the same things as:
*(tempstr + strlen(tempstr)) = '\0'
Just a different way of writing it.
However, if tempstr is NOT a null terminated string, the length would be 128 as well.
If tempstr is NOT a null terminated string then strlen(tempstr)
is undefined ('undefined' means that it's meaningless and dangerous, a bug, and shouldn't be used): the strlen
function isn't valid except when it's used on a string that's already null-terminated.