For better understanding I will shuffle the answering order:
2 - array
is a pointer of type void
. In C, pointers may be assigned to and from pointers of type void*
. Any pointer to an object may be converted to type void*
without loss of information. If the result is converted back to the original pointer type, the original pointer is recovered.
1 - It does not work the same for single elements, you do not have a generic type that can be assigned to any type. So the code is switching the content of pointed memory.
3 - n
is the number of elements in array, while size
is the size of a single element in the array. stride = size * sizeof(char);
means stride
is equal to size
, as sizeof(char)
equals 1. The size of the array sizeof(array)
equals n * size
- the number of elements in the array multiplied with the size of an element. Since both i
and j
are less than n
, i * stride
and j * stride
will never be greater than the memory used by the array. I wonder though why the use of this stride
, as far as I know sizeof(char)
is always 1.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…