我一直收到一个关于malloc的错误,我正试图找出如何在头中不使用stdlib.h的情况下让这段代码工作。只是stdio.h,这可能吗?怎么可能?我完全糊涂了
#include <stdio.h>
void allocate(int* score_array, const int input)
{
int iter;
for(iter = 1;iter <= 11;++iter)
{
if( (input < iter*10) && (input >= (iter-1)*10 ) )
{
++(score_array[iter-1]);
}
}
}
void printf_star(const int len)
{
int iter;
for(iter = 0;iter < len;++iter)
{
printf("*");
}
printf("\n");
}
int main()
{
int iter, size, temp;
int* buffer;
int score_array[11];
for(iter = 0;iter < 11;++iter)
{
score_array[iter] = 0;
}
printf("How many grades will you be entering?\n");
printf("Enter a number between 1 and 100: ");
scanf("%d", &size);
buffer = (int*)malloc(size*sizeof(int));
for(iter = 1;iter <= size;++iter )
{
printf("Getting grade %d. You have %d grade(s) left to enter\n", iter, size-iter+1);
printf("Enter a number between 0 and 100: ");
scanf("%d",&temp);
if( (temp>=0) && (temp <= 100) )
{
buffer[iter-1] = temp;
}
else
{
do
{
printf("Invalid Value!\n");
printf("Getting grade %d. You have %d grade(s) left to enter\n", iter, size-iter+1);
printf("Enter a number between 0 and 100: ");
scanf("%d",&temp);
}
while( (temp < 0) || (temp > 100) );
}
}
for(iter = 1;iter <= size;++iter)
{
allocate(score_array, buffer[iter-1]);
}
for(iter = 0;iter < 11;++iter)
{
printf_star(score_array[iter]);
}
return 0;
}
我一直有这个错误:
hw08.c: In function ‘main’:
hw08.c:56: warning: incompatible implicit declaration of built-in function ‘malloc’
最佳答案
这只是一个警告,不是一个实际的错误,所以程序仍然可以编译。
要消除警告,可以在文件中声明malloc:
#include <stdio.h>
extern void * malloc(unsigned long);
你也可以加入stdlib.h,除非你有一个重要的理由不这么做。
关于c - 我如何在不使用stdlib.h的情况下执行此代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43787270/