문제

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