1-1、问:"char a[10]; int len = strlen(a);",len等于多少?
解析:strlen计算从数组a的第一个元素开始到'\0'总共有多少个字符(不包含'\0')。
由于字符数组没有初始化,数组元素的值是未知的,所以len的值也是未定的。
1-2
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 void fun(char str[100])
6 {
7 printf("%d\n", sizeof(str));
8 // Note: 数组作为函数形参,char str[100]等价于char *str
9 // 此处为 指针的大小,结果为8字节 (64bits系统)
10 }
11
12 int main(void)
13 {
14 char str[] = "hello";
15 char *p1 = str;
16 int n = 10;
17 char *p2= (char *)malloc(100);
18
19 printf("%d\n", sizeof(str));
20 printf("%d\n", strlen(str));
21
22 printf("%d\n", sizeof(p1)); // Note: 指针变量的长度
23 printf("%d\n", strlen(p1));
24
25 printf("%d\n", sizeof(n)); // sizeof(int)
26 printf("%d\n", sizeof(p2));
27
28 fun(p2);
29
30 printf("\n");
31 return 0;
32 }
1-3、结构体struct、共用体union 的sizeof
附:字节对齐
eg. struct A{ short s; int i; char c; };
sizeof(A) = 12
= (2 + 2)+ 4 +(1 + 3)
eg. struct A{ char c; short s; int i; };
sizeof(A) = 8
= (1 + 2 + 1)+ 4
eg. union B{ double d; char c; }
sizeof(B) = 8
eg. union B{double d; char c[13]; }
sizeof(B) = 16;
2、
重载:函数名相同,参数不同
覆盖:基类的函数为虚函数(virtual),派生类的成员函数覆盖基类中的同名同参函数
隐藏:函数名相同--若 参数不同,无论基类中是否为虚函数,都将隐藏;若 参数相同,不为虚函数,则隐藏