求助!字符串变量乱码问题
我写了一个函数load,传参filename和password,这两个参数都要经过rtspace()做去首尾空格的操作
通过if语句,判断password不为空时才做去首尾空格
但是我发现,这个函数的输出是
before: the filename is : test
before: the password is : 1234
render: the password is : 1234
after: the filename is : test
after: the password is : 铵(??
即乱码,请问大家这是为什么??
为什么出了if语句的作用域,我的pw变量就乱码了?
代码如下:
int load(char* filename,char* password)
{
int len = strlen(fileName);
char buf[len-1];
strcpy(buf,fileName);
char *fn = rtspace(buf);
char *pw = NULL;
printf("before: the filename is : %s\n",fileName);
printf("before: the password is : %s\n",password);
/* int len1 = strlen(password);
char buf1[len1-1];
strcpy(buf1,password);
char* pw = rtspace(buf1);*/
if(password)
{
int len1 = strlen(password);
char buf1[len1-1];
strcpy(buf1,password);
pw = rtspace(buf1);
printf("render: the password is : %s\n",pw);
}
printf("after: the filename is : %s\n",fn);
printf("after: the password is : %s\n",pw);
}
[解决办法]
pw = rtspace(buf1);
buf1在if语句块后销毁了
还是得用strcpy
[解决办法]