// addpntrs.cpp -- pointer addition
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
using namespace std;
//4.1 array
void CreateArray() {
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = { 20,30,5 };
//{}列表初始化初始化数组可以防止缩窄
long plifs[] = { 25,92,3.0 };
char slifs[4] = { 'h','i',1222222,'\0' };
}
void CreateC_String() {
//这个不是一个字符串,cout打印会一直打印到'\0'为止
char dog[5] = { 'b','c','e','f', 'g'};
char cat[5] = { 'b','c','e','f','\0' };
char catOther[] = "bcef";//和cat的一样,字符串常量或字面值
char cat_1[5];
cout << dog << endl;
cout << cat << endl;
//'s'和"s"的区别
char shrit_size = 's'; //'s'是83 的另一种写法
char shrit_array[] = "s";//"s"是's'和'\0'的数组的地址,
//数组的长度 sizeof
int length = sizeof(cat);
//储存在数组中字符串的长度
int strLen = strlen(cat);
//赋值
//strcpy(cat_1, cat);//copy cat to cat_1
//拼接
//strcat(cat, cat_1);//append cat_1 to cat
}
void CreateString() {
string str = "panther";
//长度
int length = str.size();
}
void CreateStruct() {
struct Student {
char name[10];
int age;
};
Student a = {"aa",10};
Student b[100];
}
void CreateEnum() {
enum Cloor {
red=1,//0
orange=2,//1
yellow=3//2
};
Cloor one= Cloor::red;
one = Cloor(1);
}
void CreatePtr() {
int a = 6;
int* ptr = &a;
int* bPtr = new int;
*bPtr = 7;
delete bPtr;
if (bPtr == nullptr) {
}
else
{
bPtr = nullptr;
}
int* psome = new int[10];
psome[0] = 10;
psome[1] = 20;
psome++;
}
int main()
{
//4.1array
CreateArray();
//4.2string C-style string
CreateC_String();
//4.3string
CreateString();
//4.4struct
CreateStruct();
//4.5union
//4.6enum符号常量化
CreateEnum();
//4.7ptr
CreatePtr();
}
chapter4 复合类型
最新推荐文章于 2024-08-07 13:46:59 发布