把const修飾的右邊部分看成一個整體,例如int const *p就變成int const(*p),也就是const修飾的是指針指向的內(nèi)容,所以內(nèi)容不能改變即*p不能變,也就是不能用*p=4這種操作。int *const p相當于int *const (p),也就是const修飾的是指針p,指針p不能改變指向,所以p=&y這種操作不對。
2018-01-19
//const
#include <iostream>
using namespace std;
int main(void)
{
//定義常量count
int count = 3;
int *p = &count;
//打印count次字符串Hello C++
for(int i = 0; i < *p; i++)
{
cout << "Hello imooc" << endl;
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//定義常量count
int count = 3;
int *p = &count;
//打印count次字符串Hello C++
for(int i = 0; i < *p; i++)
{
cout << "Hello imooc" << endl;
}
system("pause");
return 0;
}
實測int *q = p;也可以實現(xiàn)指針引用。
-------------------------
確定是引用不是賦值?
指針p 賦值給指針q
-------------------------
確定是引用不是賦值?
指針p 賦值給指針q
2018-01-05
//const
#include <iostream>
using namespace std;
int main(void)
{
//定義常量count
const int count = 3;
const int *p = &count;
//打印count次字符串Hello C++
for(int i = 0; i < 3; i++)
{
cout << "Hello imooc" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//定義常量count
const int count = 3;
const int *p = &count;
//打印count次字符串Hello C++
for(int i = 0; i < 3; i++)
{
cout << "Hello imooc" << endl;
}
return 0;
}
#include <iostream>
#include<stdlib.h>
using namespace std;
int main(void)
{
int x = 3;
int &y = x;
cout<<x<<y<<endl;
//打印x和y的值
y = 1;
//修改y的值
//再次打印x和y的值
cout<<x<<y<<endl;
system("pause");
return 0;
}
#include<stdlib.h>
using namespace std;
int main(void)
{
int x = 3;
int &y = x;
cout<<x<<y<<endl;
//打印x和y的值
y = 1;
//修改y的值
//再次打印x和y的值
cout<<x<<y<<endl;
system("pause");
return 0;
}
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
char *str =new char[100];
strcpy(str, "Hello imooc");
cout << str <<endl;
delete []str;
str == NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
char *str =new char[100];
strcpy(str, "Hello imooc");
cout << str <<endl;
delete []str;
str == NULL;
return 0;
}