指针调用数组元素
指针调用数组元素
int a[3][4] = {{1,4,7,10},{2,5,8,11},{3,6,9,12}};int* pa[3] = { a[0],a[1],a[2] };int(*pb)[4]; pb = a;int** q = pa;cout << *(a[2] + 3)<<endl;cout << *(*(pa + 2) + 3)<<endl;cout << pb[2][3]<<endl;cout << *(pb + 2)[3] << endl;//错误cout << *(*(pb+2)+3) << endl;cout << q[2][3]<<endl;if(x%2)中x的性质int x = -5;//表示奇数if (x % 2) {x = 1;}cout << x;cin.getline()的用法char a[5];cin.getline(a, 5);//geiline最多读取4个元素,末尾补\0cout << a;函数参数函数不一定有形参,可以为void 如果有形参,可以是常量,变量,也可以是表达式数组名作函数参数如void sz(char a[]);char a[] = "abcd";/ sz(a);#1:实参是常量res = my_min(1, 2)#2:实参是变量a = 1b = 2res = my_min(a, b)#3:实参是表达式res = my_min(10 * 2, 10 * my_min(3, 4))#4:实参可以是常量、变量、表达式的任意组合a = 2my_min(1, a, 10 * my_min(3, 4))
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };int* p = a + 3;cout << p[5];
结果为9
p的地址为a[3]的地址,p[5]即(p+5)为a[8]的地址
故结果为9int x = 0, s = 0;while (!x != 0){s += ++x;}cout << s;
s 的值为1 条件循环的意思是x==0
#不能参与构成标识符
char str[] = "mess\15\t\\efghi\n";cout << sizeof(str);
结果为14 mess 4个字节 efghi 5个字节 \15 \t \\ \n各一字节 注意末尾的\0
不主动设置大小,数组会自动在末尾补\0
//要输出Anhui HFUTchar str1[80] = "Anhui province";char str2[80] = "my HFUT";//strcpy(str1+6, str2+3);strcpy(str1 + 5, str2 + 2);cout << str1;
strcpy(str1 + 5, str2 + 2)
str1 + 5表示str1[5]的地址 str2+2表示str2[2]的地址
将str2[2]及以后的内容复制到str1[5]及其以后
也可以用注释的第二种方法
声明函数指针: 数据类型(*函数指针名)(形参表)
指向一个函数,调用函数
利用定义的指针p,给如下定义的结构类型动态分配(用new运算符)20个struct 对象并使用
struct student {char name[5];int ID;float grade;
};
struct student* p;
int main(void) {p = new struct student [2];if (p == NULL){exit(1);}int i = 0;for (i = 0; i < 2; i++){cin >> p[i].grade >> p[i].ID;cout << endl;cin.getline(p[i].name, 5);}cout << p[1].name<<endl << p[1].grade<<endl << p[1].ID;delete[]p;p = NULL;return 0;
}
int a = 1, b = 0, c;
c = a - 1 && ++b;
cout << a << "," << b << "," << c;
结果是1,0,0 a的值不会变
ifstream inFile;
inFile.open("s.txt",std::in)以只读的方式打开文件s.txt
算法的特征:有穷性、确定性、输入性、输出性、可行性
cout << 0x13 && 0x17; 结果是19,也就是16进制的0x13