首页 > 开发 > C++ > 正文

C++ 中碰到的一个类型转换问题

2017-09-11 21:35:56  来源: 网友分享
char* askForAString(void){	char temp[16] = ""; //这里是第81行	//scanf("%s", temp);	//fflush(stdin);	return temp;}
char password[16] = "evlos";char inputpsw[16];inputpsw = askForAString(); //这里是第100行if (strcmp(password, inputpsw) == 0){	printf("Allowed!");}
projectv0.cpp: In function ‘char* askForAString()’:
projectv0.cpp:81: warning: address of local variable ‘temp’ returned
projectv0.cpp:100: error: incompatible types in assignment of ‘char*’ to ‘char [16]’

请问如何修改才能让程序正常运行?
再请问 “warning: address of local variable ‘temp’ returned” 这个错误是怎么产生应该如何避免呢?

谢谢啦 =w=

解决方案

#include <iostream>#include <list>#include <string>std::string askForAString(void){	std::string temp;	getline(std::cin, temp);	return temp;}int main(int argc, char* argv){	std::string password("evlos");	if ( 0 == password.compare(askForAString()) ) {		std::cout << "OK" << std::endl;	}	system("pause");}