【发布时间】:2018-05-25 10:07:00
【问题描述】:
我想编写一个包含冒泡排序的程序,并在函数内使用指针。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void rendez(int* t, int n){
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
if(*t+i<*t+j){
int temp = *t+i;
*t+i = *t+j;
*t+j = temp;
}
}
}
}
int main(){
setlocale(LC_ALL,"");
int t[10] = {2,3,4,5,6,7,8,9,10,11};
rendez(&t,sizeof(t));
printf("\n");
system("pause");
}
它给了我这些错误:
C:\Users\metal\gyakorlás1211.cpp In function 'void rendez(int*, int)':
C:\Users\metal\gyakorlás1211.cpp [Error] lvalue required as left operand of assignment
C:\Users\metal\gyakorlás1211.cpp [Error] lvalue required as left operand of assignment
C:\Users\metal\gyakorlás1211.cpp In function 'int main()':
C:\Users\metal\gyakorlás1211.cpp [Error] cannot convert 'int (*)[10]' to 'int*' for argument '1' to 'void rendez(int*, int)'
谢谢!
【问题讨论】:
-
你可能是指
*(t + i)等。或者只是t[i]。 -
t[i]有什么问题? -
<iostream>。这是 C++,不是 C。 -
std::sort 有什么问题?
-
我认为问题是针对
C被编辑为C++的语言
标签: c++ function pointers bubble-sort