First, I'm aware of another threads on this matter, like this one.
Unfortunately to me, the explanations are not very clear, and the results from the tests I provided are confusing me further. Let's start from the very begining with this function.
The function definition is:
size_t strxfrm(argument 1, argument 2, argument 3);
Where:
size_t
is the integer type of the value, returned by the function.
argument 1
is value of type "char *", and serves as destination.
argument 2
is value of type "const char *", and serves as a source.
argument 3
is integer of type size_t, and determines how many elements from "argument 2" will be copied into "argument 1", overwriting the values there.
So far - so good.
But by definition, the function returns
"The length of the transformed string, not including the terminating null-character."
By "transformed string" I understant "the destination" i.e. "argument 1.". The problem is, when I test the return value - it displays the length of "argument 2" i.e. "the source". For example:
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[100] = "Hello, World!", arr2[] = "Baxlazazasad";
int retValue = strxfrm(arr1, arr2, 3);
printf("Content of arr1:\t%s\nLength of arr1:\t%lu\n\nContent of arr2:\t%s\nLength of arr2:\t%lu\n\n", arr1, strlen(arr1), arr2, strlen(arr2));
printf("retValue =\t%i\n", retValue);
return 0;
}
Output:
Content of arr1: Baxlo, World!
Length of arr1: 13
Content of arr2: Baxlazazasad
Length of arr2: 12
retValue = 12
My second question regarding the function "strxfrm()", is about it's action. It is clear that the function simply copies "argument 3"-count of symbols from "argument 2" into "argument 1". Why is then the function considered "function for string compare" and not for "string copying"?
Why is then the function considered "function for string compare" and not for "string copying"?
Who is considering it this way? It is wrong. It copies and transforms. The result of transformation is in a "form such that the result of strcmp(3) on two strings that have been transformed with strxfrm() is the same as the result of strcoll(3) on the two strings before their transformation."
and the results from the tests I provided are confusing me further.
It is because the example is invalid. As documentation says: " The strxfrm() function returns the number of bytes required to
store the transformed string in dest excluding the terminating
null byte ('\0'). If the value returned is n or more, the
contents of dest
are indeterminate."
The correct example:
int main(void)
{
char arr1[100] = "Hello, World!", arr2[] = "Baxlazazasad";
size_t retValue = strxfrm(arr1, arr2, 3);
if(retValue >= 3)
{
printf("There result of this operation is indeterminate\n");
}
else
{
printf("Content of arr1:\t%s\nLength of arr1:\t%zu\n\nContent of arr2:\t%s\nLength of arr2:\t%zu\n\n", arr1, strlen(arr1), arr2, strlen(arr2));
printf("retValue =\t%zu\n", retValue);
}
return 0;
}