关闭

Sample 4.20:ptrstr.cpp

265人阅读 评论(0) 收藏 举报
分类:
#include<iostream>
#include<cstring>
int main()
{
    using namespace std;
    char animal[20] = "bear";
    const char * bird = "wren";
    char * ps;

    cout << animal << " and ";
    cout << bird << "\n";

    cout << "Enter a kind of animal: ";
    cin >> animal;

    ps = animal;
    cout << ps << "s!\n";
    cout << "Before using strcpy(): \n";
    cout << animal << " at " << (int *) animal << endl;
    cout << ps << " at " << (int *) ps << endl;

    ps = new char [strlen(animal) + 1];
    strcpy(ps, animal);
    cout << "After using strcpy(): \n";
    cout << animal << " at " << (int *) animal << endl;
    cout << ps << " at " << (int *)ps << endl;
    delete [] ps;
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37998次
    • 积分:991
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:63篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论