سؤال

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