首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

坐等:C++求高手指导一个有关问题

2013-08-09 
坐等:C++求高手指导一个问题#includeiostreamusing namespace stdint main(){char *strTempNULLchar

坐等:C++求高手指导一个问题
#include<iostream>
using namespace std;
int main()
{
char *strTemp=NULL;
char bufDest[20]={0};
int i=4;
strTemp=bufDest;
    char *strSrc="hello";
strTemp=strcpy(bufDest,strSrc);
if(strTemp==strSrc)
cout<<i<<endl;
return 0;
}在调试到if语句的时候出现问题,没有执行cout,而是直接return了,这是为什么
[解决办法]
strTemp和strSrc两个指针比较显然是不相等的,要判断字符串是否相等应该用strcmp函数。

引用:
#include<iostream>
using namespace std;
int main()
{
char *strTemp=NULL;
char bufDest[20]={0};
int i=4;
strTemp=bufDest;
    char *strSrc="hello";
strTemp=strcpy(bufDest,strSrc);
if(strTemp==strSrc)
cout<<i<<endl;
return 0;
}在调试到if语句的时候出现问题,没有执行cout,而是直接return了,这是为什么

[解决办法]

//  if(strTemp==strSrc)   // 你这里比较的只是地址而已!所以肯定不一样的,
    if(strncmp(strTemp, strSrc) == 0) // 字符串不能直接用等于号的来比较内容的
        cout<<i<<endl;
    return 0;

热点排行