我在用C建立气泡时遇到了一些问题。我试图让它在整理之前,之中和之后将其吐出阵列,而我遇到了问题。值得一提的是,这将是我的第一个C语言程序。以下是我正在从事的工作。
#include <stdio.h>
#define MAX 9
void printValues();
void sort();
void swap(int*, int*);
int values[] = {7, 3, 9, 4, 6, 1, 2, 8, 5};
void main(){
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return(0);
}
void printValues(){
int i;
printf(" List before arranging: \n[");
for (i = 0; i < 9; ++i){
printf(" %d "values[i]);
}
printf("]\n");
}
void sort(){
int i, j;
for (i = 0; i < 9; i++){
for (j = 0; j< 9-i; j++){
if (values[j] < values[j+1]){
swap(values[j]*, values[j+1]*);
}
}
}
}
void swap(int x, int y){
int temp = *x;
*x = *y;
*y = temp;
}
当然,我在此之前运行了它,并清除了我在python3中从我的时代中学到的一些错误。但是,现在我收到了。
bubble.c:22:2: warning: ‘return’ with a value, in function returning void [enabled by default]
return(0);
^
bubble.c: In function ‘printValues’:
bubble.c:29:16: error: expected ‘)’ before ‘values’
printf(" %d "values[i]);
^
bubble.c: In function ‘sort’:
bubble.c:39:20: error: expected expression before ‘,’ token
swap(values[j]*, values[j+1]*);
^
bubble.c:39:34: error: expected expression before ‘)’ token
swap(values[j]*, values[j+1]*);
^
bubble.c: At top level:
bubble.c:45:6: error: conflicting types for ‘swap’
void swap(int x, int y){
^
bubble.c:11:6: note: previous declaration of ‘swap’ was here
void swap(int*, int*);
^
bubble.c: In function ‘swap’:
bubble.c:46:13: error: invalid type argument of unary ‘*’ (have ‘int’)
int temp = *x;
^
bubble.c:47:2: error: invalid type argument of unary ‘*’ (have ‘int’)
*x = *y;
^
bubble.c:47:7: error: invalid type argument of unary ‘*’ (have ‘int’)
*x = *y;
^
bubble.c:48:2: error: invalid type argument of unary ‘*’ (have ‘int’)
*y = temp;
^
我正试图摆脱指针的束缚,无法为我遗留的问题寻求帮助。有帮助吗?
我认为您将数组和指针混合在一起,因为您编写了值[j] *而不是(values + j,并且您传递了整数而不是inn指针