Is this a correct and portable way of checking if 2 c-strings overlap in memory?

Yes, your code is correct. If two strings end at the sample place they by definition overlapped – they share the same null terminator. Either both strings are identical, or one is a substring of the other.

Everything about your program is perfectly well-defined behaviour, so assuming standards-compliant compilers, it should be perfectly portable.

The relevant bit in the standard is from 6.5.9 Equality operators (emphasis mine):

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)