字符数组和字符串数组的区别

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    char str[] = {'h','e','l','l','0'};
    char str1[] = "hello";
    cout<<"size of str: " << sizeof(str) <<endl;//5
    cout<<"size of str1: " << sizeof(str1) <<endl;//6
    return 0;
}

字符串比字符数组多了一个结束符\0

再次提醒,给字符串分配内存的时候一定要多分配一个字节

results matching ""

    No results matching ""