關閉。這個問題需要細節或清晰。它目前不接受答案。
想改進這個問題?通過編輯此帖子添加詳細資訊并澄清問題。
3天前關閉。
改進這個問題我正在嘗試創建一個登錄/注冊專案,但我很難char* tempUsername從這段代碼中宣告(SIGSEVG 分段錯誤)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
我很難理解關于指標的任何事情,所以任何建議都非常有幫助!
uj5u.com熱心網友回復:
char *tempUsername
std::cin>>tempUsername;
這里的問題是您的指標未初始化。當您嘗試從輸入流中提取到未初始化的指標時,程式的行為將是未定義的。不要這樣做。
您的目標似乎是讀取一串用戶輸入。我可以推薦的一個解決方案是使用std::string該類:
std::string tempUsername;
std::cin >> tempUsername;
無需使用指標來實作這一點。
我可以使用 char* 作為字符陣列,是這樣嗎?
一般來說,這不是真的。如果你有一個char*指向陣列元素的char,那么你可以使用它char*作為迭代器來訪問陣列的元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462784.html
