C++第一部分基础语法(指针,结构体)

这里是指针 ,结构体部分

1指针

1.1指针

#include<iostream>
using namespace std;
int main()
{
	int a = 10;

	int *p;

	p = &a;


	cout << "a的地址为:" << &a << endl;
	cout << p << endl;
	cout << *p << endl;
	cout << &p << endl;

	*p = 1000;
	cout << "修改后" << endl;
	cout << a << endl;
	cout << *p << endl;

	cout << sizeof(int *) << endl;
	cout << sizeof(p) << endl;
	cout << sizeof(*p) << endl;
	/*****************空指针*************************/
	int *p = NULL;
	*p = 1000;

	/********************野指针*********************/

	int *p = (int*)0x1100;

	cout << *p << endl;
	/**************const修饰指针**************************/


	int a = 10;
	int b = 20;
	const int *p = &a;//常量指针
	int *const p = &a;//指针常量
	const int *const p = &a;
	/******************指针和数组***************************/
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int *p;
	p = arr;
	cout << p << endl;
	cout << arr[0] << endl;
	cout << "循环打印" << endl;
	for (int i = 0; i < 9; i++)
	{
		cout << *p << endl;
		p++;
	}
	/****************************************************/

	system("pause");
	return 0;

}

1.2指针和函数


void swap01(int num1, int num2)
{
	int flag = num1;
	num1 = num2;
	num2 = flag;
	cout << num1 << endl;
	cout << num2 << endl;
}

void swap02(int *p1, int *p2)
{
	int flag = *p1;
	*p1 = *p2;
	*p2 = flag;
	cout << "指针" << endl;
	cout << *p1 << endl;
	cout << *p2 << endl;
}


int main()
{

	int a = 10;
	int b = 20;
	/*swap01(a, b);*/
	swap02(&a, &b);
	cout << a << endl;
	cout << b << endl;

	system("pause");
	return 0;
}

冒泡升序

 //封装一个函数,利用冒泡排序,实现对整形数组的升序排序
void sort(int *p,int *p2);
int main()
{
	int arr[10] = { 4, 3, 6, 9, 1, 2, 10, 8, 7, 5 };
	int num = sizeof(arr) / sizeof(arr[0]);//元素个数
	sort(arr,&num);//传数组地址和元素个数

	for (int k = 0; k < num; k++)
	{
		cout << arr[k] << endl;
	}

		system("pause");
		return 0;

}
void sort(int *p,int *p2)
{
	for (int i = 0; i <10; i++)
	{
		for (int j = 0; j < *p2 - i - 1; j++)
		{
			if (*(p + j)>*(p + j + 1))
			{
				int flag = *(p + j);
				*(p + j) = *(p + j + 1);
				*(p + j + 1) = flag;
			}
		}
	}


}

2 结构体

2.1 结构体

#include<iostream>
using namespace std;
#include<string>
#include<ctime>

//struct student
//{
//	string name;
//	int age;
//	int score;
//};
struct student
{
	string name;
	int age;
	int score;
}s3;


int main()
{
	struct student s1;//struct可以省略
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;
	cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;

	struct student s2 = { "李四", 19, 90 };
	cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;

	s3.name = "王五";
	s3.age = 20;
	s3.score = 80;
	cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
	//结构体数组
	struct student arr[2] =
	{
		{"嫖客",23,100},
		{"胖子",22,100}
	};
	for (int i = 0; i < 2; i++)
	{
		cout << "姓名:" << arr[i].name << "年龄:" << arr[i].age << "分数:" << arr[i].score << endl;
		//cout << "姓名:" << arr[1].name << "年龄:" << arr[1].age << "分数:" << arr[1].score << endl;
	}



	system("pause");
	return 0;
}

2.2 结构体指针

/*************************************结构体指针**********************************************************/
定义学生结构体
struct student
{
	string name;
	int age;
	int score;
};
int main()
{
	//创建学生的结体变量
	struct student s = { "李四", 19, 90 };
    //通过指针指向结构体变量
	struct student *p = &s;
	//通过指针访问结构体变量中的数据
	cout << p << endl;
	cout << p->age << endl;
	cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
	system("pause");
	return 0;
}

2.3 结构体嵌套结构体


struct teacher
{
	int id;
	string name;
	int age;
	struct student stu;

};

struct student
{
	string name;
	int age;
	int score;
};

int main()
{
	struct teacher t;
	t.id = 10000;
	t.name = "老王";
	t.age = 40;
	t.stu.name = "胖子";
	t.stu.age = 20;
	t.stu.score = 60;
	
	system("pause");
	return 0;
}

2.4结构体作为函数参数

struct student
{
	string name;
	int age;
	int score;
};

void print(struct student stu)
{
	cout <<"值传递:"<< "姓名:" << stu.name << "年龄:" << stu.age << "分数:" << stu.score << endl;
}
void print1(struct student *p)
{
	cout << "地址传递:" << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
}


int main()
{
	struct student stu1 = { "张三", 14, 80 };
	struct student stu2;
	stu2.name = "李四";
	stu2.age = 15;
	stu2.score = 90;
	print(stu2);
	print1(&stu2);
	//cout << "姓名:" << stu2.name << "年龄:" << stu2.age << "分数:" << stu2.score << endl;



	system("pause");
	return 0;
}

2.5const使用场景

struct student
{
	string name;
	int age;
	int score;
};

void print(const struct student *stu)//采用地址传递能节省内存,只要4个字节,采用const防止误修改了这里的值,而导致main函数的输出修改了
{
	stu->age = 200;//采用了const后,这里的值改了,就会报错,防止误操作,这就是结构体中const的用法
	cout << "地址传递:" << "姓名:" << stu->name << "年龄:" << stu->age << "分数:" << stu->score << endl;
}

int main()
{
	struct student s = { "张三", 15, 70 };
	print(&s);
	cout<<"main中: " << "姓名:" << s.name << "年龄:" << s.age << "分数:" << s.score << endl;


	system("pause");
	return 0;
}

2.6结构体案例1

//每个老师五个学生,有三个老师
//设计老师和学生的结构体,在老师的结构体中,有老师姓名和一个存放五个学生的数组作为成员
//学生的成员有姓名,考试分数,创建数组存放三个老师,通过函数给每个老师及所带的学生赋值
//打印老师数据和老师所带学生数据。

struct student
{
	string name;
	int score;
};


struct teacher
{
	string name;
	struct student sarray[5];

};


void  allocatespace(struct teacher  tarray[3],int len)
{
	string nameseed = "ABCDE";
	for (int i = 0; i < len; i++)
	{
		tarray[i].name = "teacher_";
		tarray[i].name += nameseed[i];
		for (int j = 0; j < 5; j++)
		{
			tarray[i].sarray[j].name = "student_";
			tarray[i].sarray[j].name = nameseed[j];
			tarray[i].sarray[j].score = rand() % 41 + 60;
		}

	}
}
void print(struct teacher tarray[],int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "老师姓名:" << tarray[i].name << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\t学生姓名:" << tarray[i].sarray[j].name << "考试分数" << tarray[i].sarray[j].score << endl;
		}
	}
}
int main()
{
	srand((unsigned int)time(NULL));
	//c创建老师的数组
	struct teacher tarray[3];
	//通过函数给老师的信息赋值,再给学生赋值
	int len = sizeof(tarray) / sizeof(tarray[0]);
	allocatespace(tarray, len);
	print(tarray, len);
	



	system("pause");
	return 0;
}

2.7 结构体案例2


struct hero
{
	string name;
	int age;
	string sex;
};

void sort(struct hero harray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (harray[j].age > harray[j + 1].age)
			{
				struct hero array = harray[j];
				harray[j] = harray[j + 1];
				harray[j + 1] = array;
			}

		}
	}
}
void print(struct hero harray[], int len)
{
	cout << "排序后:" << endl;
	for (int k = 0; k < len; k++)
	{
		cout << "姓名:" << harray[k].name  << " 年龄:" << harray[k].age << "  性别:" << harray[k].sex << endl;
	}
}
int main()
{
	struct hero harray[5] =
	{
		{ "刘备", 23, "男" },
		{ "关羽", 22, "男" },
		{ "张飞", 20, "男" },
		{ "赵云", 21, "男" },
		{ "貂蝉", 19, "女" },
	};
	int len = sizeof(harray) / sizeof(harray[0]);
	sort(harray, len);
	print(harray,len);
	

	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值