IntelliSense: "const void *" 类型的值不能用于初始化 "const int *" 类型的实体
程序代码://读取、排序和打印一列整型值
#include<stdio.h>
#include<stdlib.h>
//该函数由‘qsort’调用,用于比较整型值
int compare_integers (void const *a,void const *b)
{
register int const *pa = a; // 4 IntelliSense: "const void *" 类型的值不能用于初始化 "const int *" 类型的实体
register int const *pb = b; // 5 IntelliSense: "const void *" 类型的值不能用于初始化 "const int *" 类型的实体
return *pa > *pb ? 1 : *pa < *pb ? -1 : 0;
}
int main(void)
{
int *qarray;
int n_values;
int i;
//观察共有多少个值
printf("How many values are there? ");
if(scanf("%d", &n_values) !=1 || n_values <= 0)
{
printf("Illegal number of values.\n");
exit(EXIT_FAILURE);
}
//分配内存,用于存储这些值
qarray = malloc( n_values * sizeof( int )); // 6 IntelliSense: 不能将 "void *" 类型的值分配到 "int *" 类型的实体
if(qarray == NULL)
{
printf("Can't get memory for that many values.\n");
exit(EXIT_FAILURE);
}
//读取这些值
for(i = 0; i < n_values; i += 1)
{
printf("?");
if(scanf("%d", qarray + 1) != 1)
{
printf("Error reading value #%d\n",i);
free(qarray);
exit(EXIT_FAILURE);
}
}
//对这些值排序
qsort(qarray, n_values,sizeof( int ), compare_integers );
//打印这些值
for( i = 0; i < n_values; i += 1)
printf("%d\n", qarray[i]);
//释放内存并退出
free(qarray);
getch();
return EXIT_SUCCESS;
}输出









