swap函数获取增量传输的问题时,使用了指针,但是在调用swap函数时,无法将参数(str)传递给该函数。警告是:传递'strcmp'的参数2使指针从整数开始而没有强制转换[默认启用]。
有人可以帮忙吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *str);
void copy (const int size, char array[3][5]);
int main(void) {
char a[5];
char b[5];
char c[5];
char dst[3][5];
scanf("%s", a);
swap(a);
strcpy(dst[0], a);
scanf("%s", b);
swap(b);
strcpy(dst[1], b);
scanf("%s", c);
swap(c);
strcpy(dst[2], c);
copy(3, dst);
return 0;
}
void copy (const int size, char array[3][5]){
int i;
char temp[5];
for (i=0;i<3;i++){
if(strcmp(array[i],array[i+1])<0)
{
strcpy(temp, array[i]);
strcpy(array[i], array[i+1]);
strcpy(array[i+1], temp);
}
}
for (i=0; i<3;i++){
printf("%s\n", array[i]);
}
return;
}
void swap(char *str){
char temp2;
int j=0;
for (;j<5;j++){
if(strcmp(str[j],str[j+1])<0){
strcpy(temp2, str[j]);
strcpy(&str[j], str[j+1]);
strcpy(str[j+1], temp2);
}
}
return;
}
问题源:交换函数的for循环中的Ist语句
void swap(char *str)
{
//code
//for_loop
if(strcmp(str[j],str[j+1])<0)
//code
}
这里的问题是strcmp将两个字符串作为其参数,但是通过使用str [j + 1]
可以给它一个字符。
因为你有
char * str; //指向字符数组的指针(字符串)
“ str [i + 1]”表示字符串str中第(i + 1)位的字符。
那就是为什么它给错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句