a.name에 james라는 스트링이 입력되어 있습니다.
이것을 다음과 같이
strcmp(a.name, "james"); 라고 하면,
다음과 같이 에러가 납니다. 어찌하오리까?
cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
1) a.name == "james" 2) strcmp(a.name.c_str(), "james")
개인적으로는 1)을 선호합니다.
---- 절취선 ----http://blog.peremen.name
C 에서 사용하는 문자열(char *)과 C++의 std::string은 같지 않습니다.
std::string 문자열을 C 함수에서 사용하려면 std::string의 member function c_str()을 사용하면 됩니다.
참고) http://www.cplusplus.com/reference/string/string/c_str/
텍스트 포맷에 대한 자세한 정보
<code>
<blockcode>
<apache>
<applescript>
<autoconf>
<awk>
<bash>
<c>
<cpp>
<css>
<diff>
<drupal5>
<drupal6>
<gdb>
<html>
<html5>
<java>
<javascript>
<ldif>
<lua>
<make>
<mysql>
<perl>
<perl6>
<php>
<pgsql>
<proftpd>
<python>
<reg>
<spec>
<ruby>
<foo>
[foo]
1) a.name == "james" 2)
1) a.name == "james"
2) strcmp(a.name.c_str(), "james")
개인적으로는 1)을 선호합니다.
---- 절취선 ----
http://blog.peremen.name
c_str
C 에서 사용하는 문자열(char *)과 C++의 std::string은 같지 않습니다.
std::string 문자열을 C 함수에서 사용하려면 std::string의 member function c_str()을 사용하면 됩니다.
참고) http://www.cplusplus.com/reference/string/string/c_str/
댓글 달기