3.5.4

3.37

const char ca[] = { 'h', 'e', 'l', 'l', 'o' };
const char *cp = ca;
while (*cp) {
    cout << *cp << endl;
    ++cp;
}

当打印完ca中所有内容后,循环并没有结束,会继续打印不知道什么东西。这显然不是我们想要的结果。

3.38
指针相减 会得到指针所指地址之间的距离
指针相加着没有任何意义 ,如果有 ,你告诉我是什么?

3.39

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

int main()
{
    string s1("hello"), s2("world");
    if (s1 == s2)
        cout << "same string." << endl;
    else if (s1 > s2)
        cout << "hello > world" << endl;
    else
        cout << "hello < world" << endl;


    const char* cs1 = "hello";
    const char* cs2 = "world";
    auto result = strcmp(cs1, cs2);
    if (result == 0)
        cout << "same string." << endl;
    else if (result < 0)
        cout << "hello < world" << endl;
    else
        cout << "hello > world" << endl;

    return 0;
}

3.40

#include <iostream>
#include <cstring>

const char cstr1[]="Hello";
const char cstr2[]="world!";

int main()
{
    constexpr size_t new_size = strlen(cstr1) + strlen(" ") + strlen(cstr2) +1;
    char cstr3[new_size];

    strcpy(cstr3, cstr1);
    strcat(cstr3, " ");
    strcat(cstr3, cstr2);

    std::cout << cstr3 << std::endl;
}

当然 上面的代码是错误的。

一些讨论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值