string char * const char *之间的互相转换

string  ->   const char *

用str的c_str()方法或者data()方法均可,这个两个方法返回值为cong char *

    string str = "hello";
    const char *a = str.c_str();
    const char *b = str.c_str();

const char *  ->  string

const char*转换为 string,直接赋值即可

string  ->  char *

vs不让用strcpy,只能用strcpy_s代替

   string str = "hello";
    int str_len = str.length();
    char *a = new char[str_len + 1];
    strcpy_s(a, str_len + 1, str.c_str());
    delete[] a;

char *  ->  string

直接赋值

const char *  ->  char *

    const char *b = "hello";
    char *a = new char[strlen(b) + 1];
    strcpy_s(a, strlen(b) + 1, b);
    cout << a << endl;
    delete[] a;    

char *  ->  const char*

直接赋值

    char *a = "hello";
    const char *b = a;
时间: 2024-10-03 14:41:47

string char * const char *之间的互相转换的相关文章

char*,string和const char*间的转换

//1.char*转string char* c ="abc"; string s(c); //2.const char*转string const char* c_s ="abc"; string s(c_s); //3.string转const char* string s ="abc"; const char* c_s = s.c_str(); //4.string转char* string s="abcde"; con

char*,const char*和string的相互转换

好久没写东西啦,发表学术文章一篇,hiahia~ 近日和小佳子编程时遇到很多转换问题,很麻烦,在网上查了很多资料. 为了以后查找方便,特此总结如下. 如果有不对的地方或者有更简单的方法,请指出~~ 1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string 直接赋值即可 const char* c_s = "abc"; string s(c_s)

char* const char* string

1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string 直接赋值即可 const char* c_s = "abc"; string s(c_s); 3. string转char* string s = "abc"; char* c; const int len = s.length(); c = new char[len

char*,const char*和string 互转

1. string转const char* 1 string s = "abc"; 2 const char* c_s = s.c_str(); 2. const char*转string 1 //直接赋值即可 2 const char* c_s = "abc"; 3 string s(c_s); 3. string转char* 1 string s = "abc"; 2 char* c; 3 const int len = s.length()

std::string stringf(const char* format, ...)

std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 version doesn't work for buf NULL/size 0, so try printing // into a small buffer that avoids the double-rendering and alloca path too... char short_b

string、const char*、 char* 、char[]相互转换

转化总结如下: 源格式 目标格式 string const char* char* char[] string NULL const char*=string.c_str(); const char*=string.c_str(); char*=<const_cast><char*>(const char*); for(int i=0;i< string.length();i++) { char[i]=string[];} const char* string =const

const char*, char const*, char*const使用时的区别

案例1: #include<iostream> using namespace std; void main(void) { // char* a 与 char a[] 的区别 char* a = "abcdef"; // a为一指针,其值可以改变.现在a指向的是一常量字符串 cout << a << endl; a = "ghijkl"; // a现在指向另一常量字符串 //a[0] = 'z'; // a指向的为常量,编译没问

const char*, char const*, char*const的区别

Bjarne在他的The C++ Programming Language里面给出过一个助记的方法: 把一个声明从右向左读. char * const cp; ( * 读成 pointer to ) cp is a const pointer to char const char * p; p is a pointer to const char; char const * p; 同上因为C++里面没有const*的运算符,所以const只能属于前面的类型. C++标准规定,const关键字放在

const char **,char *const *,char ** const

1.告警 test_cpp.c #include <stdlib.h> #include <stdio.h> int main() { char c = 'c'; char c_q = 'c'; char *p = &c; char *q = &c_q; char **pp = &p; const char **cpp = &p; char const **c_pp = &p; char * const *pcp = &p; char