用c语言写一段代码,要求如下:/** * @brief Print an array whose elements can be any type. Each element is printed by a function whose name is an argument. * * @param arr The address of the first element of an array. * @param elemNum The number of elements in the array. * @param elemSize The size (number of bytes) of each element of the array. * @param printElem A pointer to a function (represent a function name). Such a function accepts the address of an element of an array and print the element, and return nothing. * @note The printElem should print something that can separate two elements. * @note We can learn from the design of the general qsort function. * @note void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); * @note https://cplusplus.com/reference/cstdlib/qsort/ */
时间: 2023-02-06 11:12:34 浏览: 170
AD7689通用驱动C语言代码
void printArray(void* arr, size_t elemNum, size_t elemSize, void (*printElem)(void*))
{
for (size_t i = 0; i < elemNum; i++)
{
printElem((char*)arr + i * elemSize);
}
}
// 使用示例:
void printInt(void* elem)
{
printf("%d ", *(int*)elem);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5, sizeof(int), printInt); // 输出 "1 2 3 4 5"
return 0;
}
阅读全文