【发布时间】:2015-03-17 20:21:38
【问题描述】:
我的函数获取一个 char 数组作为输入。如果它包含字符e,它将用a 更改它并返回新的字符数组。这是我的代码:
char echanger(char word[]){
int total = 0;
int i;
char final[5];
for(i=0;i<5;i++){
if(word[i]=='e'){
final[i] == 'a';
}
else{
final[i] == word[i];
}
}
return final;
}
我在 main() 函数中这样调用它:
int main(){
char a[] = "helle";
printf("new string is: %d \n",echanger(a));
}
它给了我这个输出:
new string is: -48
我在这里缺少什么?
【问题讨论】:
-
我假设 final[i] == 'a';必须是 final[i] = 'a'; (ASSIGN 不是 EVAL)
-
你的函数被声明为返回
char,但你返回的是一个char数组。 -
您还试图返回一个局部变量,该变量在函数结束时被销毁。