C 格式化问题,为什么这个错误没有消失?
我不明白什么是错误。也许是分割错误?我想让这段代码运行起来,然后找出如何使它更快。下面是错误代码。我目前正在GDB在线上调试它 ain.c:26:13:警告:格式“%d”要求参数类型为“int*”,但参数2的类型为“int”[-Wformat=]C 格式化问题,为什么这个错误没有消失?,c,C,我不明白什么是错误。也许是分割错误?我想让这段代码运行起来,然后找出如何使它更快。下面是错误代码。我目前正在GDB在线上调试它 ain.c:26:13:警告:格式“%d”要求参数类型为“int*”,但参数2的类型为“int”[-Wformat=] #include <stdio.h> #include <time.h> #include <stdlib.h> #define SIZEOFLIST 1000000000 int is_sorted(int
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZEOFLIST 1000000000
int is_sorted(int values[], int length);
void quicksort(int* numbers, int low, int high);
int partition(int* vals, int low, int high);
void swap(int *a, int *b);
int main() {
int itemsSale = 0;
int minCharge = 0;
int *list;
// Allocate space for our list.
list = (int*)malloc(SIZEOFLIST*sizeof(int));
scanf("%d%d", &itemsSale, &minCharge);
for(int i =0 ; i < itemsSale; i++){
scanf("%d", list[i]);
}
quicksort( list, 0, minCharge-1 );
free(list);
return 0;
}
void quicksort(int* numbers, int low, int high) {
// Only have to sort if we are sorting more than one number
if (low < high) {
int split = partition(numbers,low,high);
quicksort(numbers,low,split-1);
quicksort(numbers,split+1,high);
}
}
int partition(int* vals, int low, int high) {
int temp;
int i, lowpos;
// Pick a random partition element and swap it into index low.
i = low + rand()%(high-low+1);
swap(&vals[low], &vals[i]);
// Store the index of the partition element.
lowpos = low;
// Update our low pointer.
low++;
// Run the partition so long as the low and high counters don't cross.
while (low <= high) {
// Move the low pointer until we find a value too large for this side.
while (low <= high && vals[low] <= vals[lowpos]) low++;
// Move the high pointer until we find a value too small for this side.
while (high >= low && vals[high] > vals[lowpos]) high--;
// Now that we've identified two values on the wrong side, swap them.
if (low < high)
swap(&vals[low], &vals[high]);
}
// Swap the partition element into it's correct location.
swap(&vals[lowpos], &vals[high]);
return high; // Return the index of the partition element.
}
// Swaps the values pointed to by a and b.
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
#包括
#包括
#包括
#定义SIZEOFLIST 100000000
int被_排序(int值[],int长度);
无效快速排序(整数*数字、整数低位、整数高位);
整数分区(整数*VAL、整数低位、整数高位);
无效掉期(int*a、int*b);
int main(){
int itemsale=0;
int minCharge=0;
int*列表;
//为我们的列表分配空间。
list=(int*)malloc(SIZEOFLIST*sizeof(int));
scanf(“%d%d”,&itemsSale,&minCharge);
for(int i=0;i 而(低当您尝试使用scanf读取int时,scanf不需要int的值,它需要它的地址。打印需要该值,但scanf需要知道在哪里存储它读取的值,因此它需要int*
这正是编译器告诉你的:你传递了一个int,但是你需要传递一个int*#定义SIZEOFLIST 100000000
假设sizeof(int)=4
,那么你的程序真的需要4GB的内存吗?scanf(%d),list[i])
-->scanf(%d),&list[i])
list[i]
是一个int
。错误表明它需要一个int*
,而不是int
scanf
格式的%d
需要一个指向int
@KamilCuk的指针,这似乎是一个家庭作业/etc问题,因此大小可能是“然后找出如何让它更快”的一部分.谢谢你用另一件事澄清了我的困惑。数字/编码很有趣…特别是当人们被问及它们时。