KChang 2021. 9. 18. 00:04

1. 포인터

const char * weekday[]= {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

여기서 const는 const char *가 아닌 const char에 적용 되므로 포인터가 가리키고 있는 값을 바꿀 수는 없지만 주소 값은 바꿀 수 있다.

 

2. string 특정 문자열로 자르고 싶을 때

#include <iostream>
#include <cstring>

using namespace std;
string str_arr[1000];
int str_cnt;

int main() {
    string a = "My name is kukaro";
    char *str_buff = new char[1000];
    strcpy(str_buff, a.c_str());

    char *tok = strtok(str_buff, " ");
    while (tok != nullptr) {
        str_arr[str_cnt++] = string(tok);
        tok = strtok(nullptr, " ");
    }

    for (int n = 0; n < str_cnt ; n++) {
        cout << str_arr[n] << endl;
    }
    return 0;
}

 

 

참고 자료