您的代码与您发布的代码完全一样,并通过我的gcc生成了这些消息
6398652.c:4:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
6398652.c: In function ‘main’:
6398652.c:4:5: error: old-style function definition [-Werror=old-style-definition]
6398652.c:18:3: error: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Werror=format]
6398652.c: In function ‘create_array’:
6398652.c:25:3: error: implicit declaration of function ‘malloc’ [-Werror=implicit-function-declaration]
6398652.c:25:21: error: incompatible implicit declaration of built-in function ‘malloc’ [-Werror]
cc1: all warnings being treated as errors
此更改的版本可以干净地编译
#include
#include
int *create_array(int n, int initial_value);
int main(void) {
int *arr;
int num;
int numOfNum;
printf("Store this integer:\n");
scanf("%d", &num);
printf("Store the integer this amount of time:\n");
scanf("%d", &numOfNum);
arr = create_array(num, 1);
if (arr == NULL) {
printf("ERROR\n");
} else {
printf("Array stored in this location: %p\n", (void*)arr);
}
return 0;
}
int *create_array(int n, int initial_value) {
int *pointer;
int i;
pointer = malloc(10 * sizeof *pointer);
for (i = 0; i < n; i++) {
int *p;
p = pointer;
p += n*(sizeof *p);
*p = initial_value;
}
return pointer;
}