是否有可能存儲在一個變量C數據類型?是否可以將C數據類型存儲在變量中?
事情是這樣的:
void* type = (byte*);
這裏有一個場景,其中我寫了一個測試案例,並嘗試使用某些數據類型printf中使用打印出一個字節數組,這取決於給定參數:
void print_byteArray(const void* expected, size_t size,
bool asChars, bool asWCharT) {
int iterations;
char* format;
if (asChars) {
iterations = (size/(sizeof (char)));
format = "%c";
} else if (asWCharT) {
iterations = (size/(sizeof (wchar_t)));
format = "%lc";
} else {
iterations = (size/(sizeof (byte)));
format = "%x";
}
int i;
for (i = 0; i < iterations; i++) {
if (asChars) {
printf(format, ((char*) expected)[i]);
} else if (asWCharT) {
printf(format, ((wchar_t*) expected)[i]);
} else {
printf(format, ((byte*) expected)[i]);
}
}
fflush(stdout);
}
這看起來像效率低下的代碼。我想像它是一個能規模下的for循環體,一條線路:
printf(format, ((type) expected)[i]);