C++ Primer 读书笔记 第四章

这一章更多的是在讲C语言的东西。

Pointers to const Objects

const double *ptr

const Pointers

double *const ptr = &value //必须初始化

const Pointer to a const Object

cosnt double *const ptr = &value

Pointers and Typedefs

Typedef string *pstring

const pstring cstr;

其实const pstring cstr 等价于 string *const cstr

const int *pci_ok = new const int[100]()  //初始化为0

delete [] pia //数组删除动态内存

char arr[0]                    //error: cannot define zero-length array

char *cp = new char[0]  //ok: but cp cannot be dereferenced

int *ip[4]                     //array of pointers to int

int (*ip)[4]                  //pointer to an array of 4 ints

Library strings and C-style Strings

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    string s("abcdefghi");
    const char *str = s.c_str(); //The array return by c_str is not guaranteed to be valid indefinitely
    s[0] = 'w';
    cout << s << endl;
    cout << str << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/null00/p/3087947.html