None of OP's 3 buffer
are best described as C strings. Simply arrays of char
.
char buffer[] = {'a', '0'}; // No terminating \0
char buffer[] = {'a', '0'}; // No terminating \0
// No terminating \0 yet has embedded \0
char buffer[] = {'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};
Since OP "cannot use any other functions" ...
Find range of valid indexable memory buffer
and p2
. The valid indexable range of buffer
is 0
to sizeof(buffer) - 1
.
The same for p2
(the string literal) is complicated. If p2
was a typical C string, the range is 0
to strlen(p2) + 1
. But a string literal such as "eabgelegon\000pknml\000jih"
has embedded '\0'
in it (as well as a terminating '\0'
), so its range cannot be determined at run time with strlen()
, but only at compile time with sizeof()
.
Let's assume the comparison should not include the string literal's terminating '\0'
.
char buffer[] = 'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};
char p2[] = "eabgelegon\000pknml\000jih"; // matches
//char p2[] = "eabgelegon\000pknml\000ji "; // fails
//char p2[] = "eabgelegon\000pk ml\000jih"; // fails
size_t buffer_size = sizeof(buffer);
size_t p2_size = sizeof(p2);
// p2 should be 1 longer than buffer due to its terminating \0
if (buffer_size + 1 != p2_size) return NOT_EQUAL;
size_t i;
for (i=0; i<buffer_size; i++) {
if (buffer[i] != p2[i]) return NOT_EQUAL;
}
return EQUAL;
Note: sizeof()
is not a function in C, but an operator.