用两个函数使用new和delete

#include
#include

using namespace std;

char *getname(void)
{
    char temp[80];
    cout << "Enter the name: ";
    cin >> temp;
    char *pn = new char[strlen(temp) + 1];
    strcpy(pn,temp);
    return pn;
}

void putname(char *name)
{
    char *dame;
    dame = name;
    cout << dame << " at " << (int *)dame << endl;
    delete [] dame;
}

int main()
{
    char *getname(void);
    void putname(char *name);
    putname(getname());
    putname(getname());
    return 0;
}

 

你可能感兴趣的:(C++)