#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請100個char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy_s(str,100,"Hello imooc");
//打印字符串
cout << str << endl;
system("pause");
//釋放內(nèi)存
delete[]str;
str = NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請100個char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy_s(str,100,"Hello imooc");
//打印字符串
cout << str << endl;
system("pause");
//釋放內(nèi)存
delete[]str;
str = NULL;
return 0;
}
這一題剛做的時候感覺沒有答案,后來想想也就想明白了,int const a = 3;表示的是定義了一個整型常亮,即改變a的值是不被計算機允許的,而A后面又定義了*p = &a,意思就是定義一個指針p,指向a,此時可以通過修改*p的值來修改a的值,然而a被定義為整型常亮,對其進行修改值得操作顯然是不被接受的,所以最起碼應(yīng)該定義為int const *p = &a,保證*p不能被改變。
2018-05-31
說一個比較好記的方法來區(qū)分 int const *p與 int* const p,把*讀作pointer to然后從后往前讀.
第一個int const *p就可以讀作 p is a pointer to const int,p是指向常量的指針
第二個int* const p就可以讀作 p is a const pointer to int,p是指向int型的常指針
第一個int const *p就可以讀作 p is a pointer to const int,p是指向常量的指針
第二個int* const p就可以讀作 p is a const pointer to int,p是指向int型的常指針
2018-05-28