void calc_probability(int num)
{
int i = , j = , k = ;
#define SIZE_NUM 8
int *array_num = NULL;
int *remember_num = NULL;
char (**array_detail)[SIZE_NUM] = NULL; //数组指针
int len_array = ;
int count_num = ;
int count = ; len_array = * num - ;
array_num = (int*)calloc(len_array, sizeof(int));
array_detail = (char (**)[SIZE_NUM])calloc(len_array, sizeof(char(*))); //这里注意是sizeof(char(*)不是sizeof(char(*)[SIZE_NUM])
remember_num = (int*)calloc(len_array, sizeof(int)); for(i = ; i < len_array; i++)
{
array_num[i] = i + ;
array_detail[i] = (char(*)[SIZE_NUM])calloc(count_num + , sizeof(char[SIZE_NUM]));
} for(i = ; i < len_array; i++)
{
count = ; // 计数清零
count_num = ;
for(j = ; j <= num; j++)
{
for(k = ; k <= num; k++)
{
if(j + k == array_num[i])
{
sprintf(array_detail[i] + count_num, "(%d, %d)", j, k);
//printf("%s", array_detail[i] + count_num);
count_num++;
count++;
array_detail[i] = (char(*)[SIZE_NUM])realloc(array_detail[i], (count_num + ) * sizeof(char[SIZE_NUM]));
memset(array_detail[i] + count_num, 0x0, SIZE_NUM);
}
}
}
remember_num[i] = count;
} for(i = ; i < len_array; i ++)
{
printf("数字 %d 的个数是: %d\n", i + , remember_num[i]);
for(j = ; j < remember_num[i]; j++)
{
printf("%s ", array_detail[i] + j);
}
printf("\n");
} //释放内存
free(remember_num);
remember_num = NULL; free(array_num);
array_num = NULL; for(i = ; i < len_array; i++)
{
free(array_detail[i]);
array_detail[i] = NULL;
}
free(array_detail);
array_detail = NULL; return; }
错了好多次,终于改对了。