#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請(qǐng)100個(gè)char類型的內(nèi)存
char *str =new char(100);
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout << str << endl;
//釋放內(nèi)存
delete []str;
str = NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請(qǐng)100個(gè)char類型的內(nèi)存
char *str =new char(100);
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout << str << endl;
//釋放內(nèi)存
delete []str;
str = NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int x = 3;
//定義引用,y是x的引用
int &y = x;
//打印x和y的值
cout << x << endl;
//修改y的值
y = 4;
//再次打印x和y的值
cout << x << endl;
return 0;
}
using namespace std;
int main(void)
{
int x = 3;
//定義引用,y是x的引用
int &y = x;
//打印x和y的值
cout << x << endl;
//修改y的值
y = 4;
//再次打印x和y的值
cout << x << endl;
return 0;
}
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請(qǐng)100個(gè)char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout<<str<<endl;
//釋放內(nèi)存
delete []str;
str=NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請(qǐng)100個(gè)char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout<<str<<endl;
//釋放內(nèi)存
delete []str;
str=NULL;
return 0;
}
5-3練習(xí),題目有問題。D中講得不時(shí)說要用,delete []p 嗎?我自己在電腦上敲了下,int *p=new int (20) delete p 同樣是對(duì)的.
2015-09-03
new <--> delete
malloc<---> free
配對(duì)使用,申請(qǐng)后,檢查是否成功。
malloc<---> free
配對(duì)使用,申請(qǐng)后,檢查是否成功。
2015-08-30
int a = 3;
int *p = &a;
原來*p和p不是同一個(gè)來的,*p和a相對(duì),而p又是另外的一個(gè)變量。
int *p = &a;
原來*p和p不是同一個(gè)來的,*p和a相對(duì),而p又是另外的一個(gè)變量。
2015-08-30
最贊回答 / onemoo
const int const *p 這個(gè)聲明是錯(cuò)誤的,這樣兩個(gè)const都是修飾int的,重復(fù)了。選項(xiàng)A中:a是一個(gè)const int變量,p是一個(gè)普通int指針,不能指向const變量。所以A是錯(cuò)的。
2015-08-30