5.9.1
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
char x = 'a';
char * ch = &x;
int num[10];
for(int i = 0; i < 10; i++)
{
num[i] = i;
}
int (&n)[10] = num;
string str[2] = {"Oh", "Don't give up"};
string (*p_s)[2] = &str;
char ** c = &ch;
const int o = 6;
const int * p_n = &o;
int * const cp = NULL;
return 0;
}
5.9.2
现代系统应该都没有这方面的限制了吧
5.9.3
题挺绕的不要在意名字看写法就行
这里教一个判断这种复杂类型的方法
例如:int (*pt[3])[4] 解读顺序为pt->[3]->星号->[4]->int
连起来就是pt是一个数组数组的每个元素是一个指针每个指针指向一个包含4个元素的数组数组元素的类型为int 总结来讲就是按照名字最先其次是优先级顺序
typedef unsigned char uchar;
typedef const unsigned char cuchar;
typedef int pint;
typedef char *pchar;
typedef char (*ppchar)[7];
typedef int *pint[7];
typedef int (*ppint)[7];
typedef int lppint[8][7];
5.9.4
void exchange1(int * p1, int * p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void exchange1(int & p1, int & p2)
{
int temp = p1;
p1 = p2;
p2 = temp;
}
5.9.5
数组的大小是所有字符数加1(’\0’)
5.9.6
合法:‘a’,49,c,sc
不合法:3300, uc
5.9.7
比较简单就直接用结构了
struct Year
{
char * Month;
int Day;
};
Year temp[12] =
{
{"一月", 31},{"二月", 28},{"三月", 31},{"四月", 30},{"五月", 31},
{"六月", 30},{"七月", 31},{"八月", 31},{"九月", 30},{"十月", 31},
{"十一月", 30},{"十二月", 31}
}
for(int i = 0; i < 12; i++)
{
cout << temp[i].Month << ' ' << temp[i].Day << endl;
}
5.9.8
使用指针和使用下标访问速度上应该是没有影响的
因为使用下标实际上就是指针访问的另一种方式
5.9.9
这道题属实没看懂题意
可以看看https://bbs.csdn.net/topics/60206436
5.9.10
void show(const char (*s)[15])
{
for(int i = 0; i < 12; i++)
{
cout << s[i] << endl;
}
}
5.9.11
#include <iostream>
#include <string>
using namespace std;
void Show(const string *);
int main(void)
{
string temp[5] =
{
"xswl", "Oh sorry", "Hi boy",
"xswl", "Quit"
};
Show(temp);
return 0;
}
void Show(const string * s)
{
int count = 0;
while(s[count] != "Quit")
{
for(int i = 0; i < count; i++)
{
if(s[i] == s[count])
{
count++;
if(s[count] == "Quit")
return;
i = 0;
continue;
}
}
cout << s[count] << endl;
count++;
}
}
5.9.12
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string x = "sibslalxsinwoc";
int count = 0;
int location = 0;
int sign;
while((sign = x.find("si", location)) != x.npos)
{
count++;
location += sign + 1;
}
cout << "si在" << x << "中出现了" << count << "次" << endl;
return 0;
}
5.9.13
这个比较简单 就是搞个结构用函数初始化和输出他 还有用相同的结构初始化他偷个懒就不写了