编译时出现警告deprecated conversion from string constant to ‘char*’ [- Wwrite-strings];
问题描述:
到底要怎么改啊,求解
c语言
#include<stdio.h>
#include<string.h>
int f(char *s[],int n)
{
char *temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(strcmp(s[i],s[j])>0){
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
return 0;
}
int main()
{
char *s[]={"good","better","well","see","luck"};
int n=5;
f(s,n);
for(int i=0;i<n;i++){
printf("%s\n",s[i]);}
return 0;
}
收
答
//把变量s,temp变成const,f的传参也变成const
#include<stdio.h>
#include<string.h>
int f(const char* s[], int n)
{
const char* temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(s[i], s[j]) > 0) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
return 0;
}
int main()
{
const char* s[] = { "good","better","well","see","luck" };
int n = 5;
f(s, n);
for (int i = 0; i < n; i++) {
printf("%s\n", s[i]);
}
return 0;
}