Given the method header

void scanArray(void *arr, int const numElements, int const sizeElement, char const *fmt)

where *arr can be any type of array, numElements is the number of elements in the array, and sizeElement is the size of the type of value in the array, and fmt is a string such as %d, %lf, or %f, how would you write a function that uses scanf to insert values into the array?

有帮助吗?

解决方案

void scanArray(void *arr, int const numElements, int const sizeElement, char const *fmt){
  int i;
  unsigned char *tempArr = (unsigned char*) arr;
  for(i=0; i<numElements; i++, tempArr+=sizeElement) scanf(fmt, tempArr);
}

This seems to be working for me...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top