const.cpp

来自「C++中定义常量以及一些语法规则,由于C++中规则较为烦琐,所以代码中也加了一定」· C++ 代码 · 共 47 行

CPP
47
字号
// const.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
const int value =  1100;
const height =1000;
const char title[]="computer science" ;
const char* p=title;

using namespace std;
void f(const char* p)
{
	//p[0]='c';
	printf("%s\n",p);
}
void g(const string& str)
{
	cout<<str<<endl;
}

int main(int argc, char* argv[])
{
	
	char s[]="bate" ;
	char m[]="make" ;
	
	const char* p=s;
	p=m;
	cout<<p<<endl;
//	p[0]='m' ;   
#if 1
	char * const p1=s;
//	p1=m;
	p1[0]='m';
	cout<<p1<<endl;
#endif
//  不能将常量的地址赋值给一个非常量的指针
//	char* p2= title;
//	int *p3=&value;

	g("Hello everyone"); //如果去掉参数声明中的const,能否编译过去?
	f("Hello everyone");
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?