#include <iostream>
using namespace std;
int main1()
{
int a1 = 10;
int b1 = 20;
// 在C语言中表达式不能作左值,为什么下列语句能编译通过并且正常运行?.
(a1 < b1 ? a1 : b1) = 40; // 在这里返回的是&a(在c语言中编译不通过).
cout << a1 << endl;
return 0;
}
struct Teacher
{
char name[128];
int age;
};
/*
int opraterTeacher(const struct Teacher *p)
{
p->age = 10;
return 0;
}
*/
/*
int opraterTeacher(struct Teacher const*p)
{
p->age = 10;
printf("age =d\n", p->age);
}
*/
void oprater(Teacher *const pt)
{
pt->age = 10;
// pt = NULL; 出错
printf("age= d\n", pt->age);
}
// 在C语言中const 是一个冒牌货,通过变量的地址可以修改变量的值
// 引用的妙处
void swap(int &a,int&b)
{
int c = 0;
c = a;
a = b;
b = c;
}
struct student
{
char name[64]; // 64
int age; // 4
int &a; // 4 类似与指针所占的内存大小
char &b; // 4
};
//引用的本质
void modifyA(int &a)
{
a = 5;
}
// 在c++编译器的将上面的引用类型做了如下改造
void modifyB(int *const a)
{
*a = 5;
}
void main()
{
int x = 1000;
int &d = x; //c++的标准语法.
//int &y; 普通引用必须初始化.
int m = 3;
int n = 2;
const int a = 10;
const int b = 10;
int array[a + b] = { 0 };
cout << sizeof(array) << endl;
const int c = 10;
int *p = NULL;
p = (int*)&c;
*p = 30;
cout << "c=" << a << "*p" << *p << endl; // 表明在c++中const 就是一个真正的常量
cout << d << endl;
cout << x << endl;
swap(m, n);
cout << m << endl;
cout << n << endl;
const int dt = 100;
cout << "sizeof(student)=" << sizeof(student) << endl;
system("pause");
//const 的基础
}