gpt4 book ai didi

c - 警告从整数生成指针而不进行强制转换

转载 作者:行者123 更新时间:2023-11-30 20:26:28 24 4
gpt4 key购买 nike

swap函数存在增广传递问题,使用了指针,但是调用swap函数时,无法将argument(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;
}

最佳答案

问题根源:swap函数的for循环中的Ist语句

  void swap(char *str)
{
//code
//for_loop
if(strcmp(str[j],str[j+1])<0)
//code
}

这里的问题是 strcmp 使用两个字符串作为参数,但是使用 str[j+1]
你正在赋予它一个角色。

自从你有了
字符*str;//指向字符数组(字符串)的指针

“str[i+1]”表示字符串str中第(i+1)个位置的字符。
这就是它给出错误的原因。

关于c - 警告从整数生成指针而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com