求解答,我看不出来怎么解决,谢谢!
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int count, *array;
if ((array(int *)malloc (10 * sizeof(int))) == NULL)
{
printf("can not success!\n");
exit(1);
}
for (count = 0; count < 10; count++)
array[count] = count;
for (count = 0; count < 10; count++)
printf("%2d ", array[count]);
return 0;
}
虽然基本问题只是少了个等号,但希望楼主学到的不只是语法,而是 C 语言的风格与习惯,以下逐步地指出能改善的地方:
if ((array = (int *)...
if ((array = malloc...
请参考 Do I cast the result of malloc?
if (!(array = malloc(...
总结以上代码:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int count = 10;
int i, *array;
if (!(array = malloc(count * sizeof(int)))) {
printf("cannot success!\n");
exit(1);
}
for (i = 0; i < count; i++)
array[i] = i;
for (i = 0; i < count; i++)
printf("%2d ", array[i]);
return 0;
}
if ((array = (int *)malloc(10 * sizeof(int))) == NULL)
{
printf("can not success!\n");
exit(1);
}