cpp数组,结构体,union
1.数组的定义与传参
int A[6];
- 定义上A就是数组类型,有sizeof(A)=6*sizeof(int)
- 当它作为参数传递时,才变成了一个指针,所以要加个长度,因此才有了数组传参时,需要传进去两个参数(数组名和数组长度)
void f(int a[], int n);
- 符号串(字符串)可以不传长度,因为末尾有‘\0’字符
void f(char a[]);
特例:
char s2[]={'a','b','c'}; cout << s2;
- 这个末尾没有、0,而是烫
typedef int T[3];
T A[2];
//相当于int A[2][3];
typedef T T1[c2];
//相当于T1 T[c2];
T1 A[c1];
T A[c1][c2]
2.结构体struct
- 赋值必须是同类型(同类型/typedef)
- 对齐:
- struct浪费了空间
3.union
- 共享存储空间
union Matrix
{
struct
{ double _a11, _a12, _a13;
double _a21, _a22, _a23;
double _a31, _a32, _a33;
};
double _element[3][3];
};
//两个不同的视角来操纵
- 多态:重载函数,union,宏