C++ 上的 strncmp 函数
strncmp function on c++
我在 c++ 中使用 strcmp 函数有问题,编译器说"读取字符串字符时出错",我确实使用了字符串.. 如果你能看一看,我会更伟大。
///这是使用函数的地方,数据是类 MailAcount//
cout << "please enter user name: " << endl;
char input_user[20];
cin >> input_user;
cout << "please enter password: " << endl;
char input_password[20];
cin >> input_password;
if (!strncmp(input_user, data.GetUser(), 20) ||
!strncmp(input_password, data.GetPassword(), 20))
cout << "ERROR" << endl;
else
{
cout << "Access confirm" << endl;
}
这是 MailAcount 的标头//
class MailAcount
{
private:
char* _email;
char* _password;
public:
MailAcount(char* email,char* password);
MailAcount();
char* GetUser();
char* GetPassword();
~MailAcount();
};
这是 MailAcount 的 cpp//
#include "MailAcount.h"
#include <iostream>
using namespace std;
MailAcount::MailAcount(char *email,char *password)
{
_email = email;
_password = password;
}
MailAcount::MailAcount()
{
}
char* MailAcount::GetUser()
{
return _email;
}
char* MailAcount::GetPassword()
{
return _password;
}
MailAcount::~MailAcount()
{
}
调试器中的程序崩溃很可能是由于您没有向邮件帐户中的指针指向的字符提供内存的情况。
这个有效
char myName[20]; // 20 characters on the stack
char myPwd[20]; // 20 characters on the stack
MailAccount(myName, myPwd); // ok, points to allocated memory (on the stack)
这个会崩溃
char* myName; // only a pointer pointing somewhere
char* myPwd; // only a pointer pointing somewhere
MailAccount(myName, myPwd); // not ok, as the pointers will point (most likely) to non accessible memory
编辑:当您将其标记为C++时,您仍然应该考虑使用标准库字符串,正如评论者已经建议的那样。编辑2:更正了注释
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 在C++中,如何在 C 中使用 strncmp 函数?
- Arduino IDE 中使用 strncmp 函数时出错
- gdb-查找strncmp()函数的值
- C++ 上的 strncmp 函数