【c++】指针+结构体

【c++】指针+结构体,第1张

指针

定义 数据类型 *p;

声明 p=地址,p为地址,*p为地址对应的值

定义并声明 数据内心 *p=地址;

  • 空指针:指针变量指向内存中编号为0的空间,用于初始化指针变量,空指针指向的内存是不可以访问的
    int *p = NULL;
  • 野指针:指针变量指向非法的内存空间,程序中避免用野指针
  • 常量指针:const int *p = &a; //指针的指向可以改,但不能通过该指针来修改指向内存的值
  • 指针常量:int * const p = &a; //指针的指向不能改,但指针指向的内存的值可以改
  • 常量指针常量:const int * const p = &a; //指针的指向和指针指向内存的值都不改

  • 注意:指针的p++,不是地址的地址(p值)单纯的+1,是加一个指针指向数据类型的长度,即下移一位元素
  • 指针与数组:可以用指针表示数组中的元素
  • 指针与函数:地址传递
    void funcname(int *p1, int *p2){}
    funcname(&a,&b)
#include 
#include 
using namespace std;

void main()(){
	int num = 10;//数
	double arr1[5] = { 1,2,3,4,5 };//一维
	int arr2[5][4] = { { 1,2,3,4 }, {6,7,8,9} };//二维
	
	int *p= #
	double *p1 = arr1;
	int *p2 = arr2[0];
	cout << arr2 << endl;
	cout << &arr2[0] << endl;

	cout << "p:" << p << endl ;
	cout << "*p:" << *p << endl;
	cout << "&num:" << &num << endl;
	cout << "num:" << num << endl << endl;
	

	cout << "p1:" << p1 << endl;
	cout << "++p1:" << ++p1 << endl;
	cout << "*p1:" << *p1 << endl;
	cout << "arr1:" << arr1 << endl;
	cout << "arr1[0]:" << arr1[0] << endl << endl;

	cout << "p2:" << p2 << endl;
	cout << "*p2:" << *p2 << endl;
	cout << "arr2:" << arr2 << endl;
	cout << "arr2[0]:" << arr2[0] << endl;
	cout << "arr2[0][0]:" << arr2[0][0]<< endl << endl;


	//32位下(x86)指针都是占4B,64位(x64)指针都是占8B
	cout << "int*所占空间:" << sizeof(int*) << endl;
	cout << "double*所占空间:" << sizeof(double*) << endl;
	cout << "char*所占空间:" << sizeof(char*) << endl;
	cout << "string*所占空间:" << sizeof(string*) << endl;
}
结构体
  • 结构体:用户自定义的数据类型,允许用户存储不同的数据类型
    struct 结构体名称 {结构体成员列表};
  • 结构体与数组:结构体名称 变量名称[];
  • 结构体与指针:stu *p1 = &s1; stu *p = &s[0];
  • 结构体嵌套结构体:
  • 结构体和函数:值传递和地址传递
    值传递会把数据复制一份增加数据量,地址传递效率高但可能导致数据的修改(可配合const使用)
//同一个项目不同的cpp文件中不要定义相同的结构体名称,结构体和类相似
#include 
#include 
using namespace std;

struct stu {
	//成员列表
	string name;
	int age;
	double score;
}s3;

struct tea{//老师结构体
	string name;
	int age;
	stu  s;
};

void Test(){
//创建结构体变量的三种方法,多用1和2
	cout << "==============结构体" << endl;
	//1
	//struct stu s1;
	stu  s1;//创建结构体变量时struct可以省略
	s1.name = "Job";
	s1.age = 18;
	s1.score = 98.2;
	cout << "name:" << s1.name << "  age:" << s1.age << "  score:" << s1.score << endl;

	//2
	struct stu  s2 = { "Tim" ,19,85.2};
	cout << "name:" << s2.name << "  age:" << s2.age << "  score:" << s2.score << endl;

	//3 在定义结构体时顺便创建结构体变量
	s3.name = "May";
	s3.age = 17;
	s3.score = 92.1;
	cout << "name:" << s3.name << "  age:" << s3.age << "  score:" << s3.score << endl;

	
//结构体与数组
	stu  s[3] = { 
		{ "Job" ,18, 98.2 }, 
		{ "Tim" ,19, 85.2 },
		{ "May" ,17, 92.1 }
	};
	cout << endl << "==============结构体与数组" << endl;
	for (int i = 0; i <= 2; i++) {
		cout << "name:" << s[i].name << "  age:" << s[i].age << "  score:" << s[i].score << endl;
	}

	cout << endl << "==============结构体所占内存空间" << endl;
	cout << "sizeof(s:)" << sizeof(s) << endl;
	cout << "sizeof(s1:)" << sizeof(s1) << endl;
	cout << "sizeof(s[1].name:)" << sizeof(s[1].name) << endl;
	cout << "sizeof(s[1].age:)" << sizeof(s[1].age) << endl;
	cout << "sizeof(s[1].score:)" << sizeof(s[1].score) << endl;

//结构体与指针
	stu  *p1 = &s1;
	cout << endl << "==============结构体与指针" << endl;
	cout << "name:" << p1->name << "  age:" << p1->age << "  score:" << p1->score << endl;

	stu  *p = &s[0];
	cout << endl << "==============结构体数组与指针" << endl;
	for (int i = 0; i <= 2; i++,p++) {
		cout << "name:" << p->name << "  age:" << p->age << "  score:" << p->score << endl;
	}

//结构体嵌套结构体
	tea t;
	t.name = "Kun";
	t.age = 48;
	t.s.name = "Job";
	t.s.age = 18;
	t.s.score = 98.2;
	cout << endl << "==============结构体嵌套结构体" << endl;
	cout << "Teachername:" << t.name << "  Teacherage:" << t.age << "  Stuname:" << t.s.name << endl;
}

void print1(stu s) {//值传递
	s.score = 78;
	cout << "子函数 name:" << s.name << "  age:" << s.age << "  score:" << s.score << endl;
}

void print2(stu *s) {//地址传递
	s->score = 78;
	cout << "子函数 name:" << s->name << "  age:" << s->age << "  score:" << s->score << endl;
}


void main(){
	Test();

	cout << endl << "==============结构体函数传递" << endl;
	stu  s1;//创建结构体变量时struct可以省略
	s1.name = "Job";
	s1.age = 18;
	s1.score = 98.2;
	cout << "主函数 name:" << s1.name << "  age:" << s1.age << "  score:" << s1.score << endl;
	cout << endl << "值传递后" << endl;
	print1(s1);//值传递
	cout << "主函数 name:" << s1.name << "  age:" << s1.age << "  score:" << s1.score << endl;
	cout << endl << "地址传递后" << endl;
	print2(&s1);//地址传递
	cout << "主函数 name:" << s1.name << "  age:" << s1.age << "  score:" << s1.score << endl;
}

结构体数组也可以像数组一样直接交换

#include 
#include 
#include
using namespace std;

struct man {
	//成员列表
	string name;
	int age;
	double atk;
};

void def(man *m,int len) {
	string str = "ABCDE";
	for (int i = 0; i < 5; i++, m++) {
		int hage_rand = rand() % 20 + 20;
		int hatk_rand = rand() % 301 + 700;
		m->name = "Hero_";
		m->name += str[i];
		m->age = hage_rand;
		m->atk = hatk_rand;
	}
}

void bubblesort(man* m, int len) {//冒泡排序
	man *p1;
	man *p2;
	for (int i = 0; i < len; i++) {
		p1 = m + i;
		for (int j = i+1; j < len; j++) {
			p2 = m + j;
			if (p1->atk < p2->atk) {
				man temp = *p2;
				*p2 = *p1;
				*p1 = temp;
			}
		}
	}
}

void print(man* m, int len) {
	for (int i = 0; i < 5; i++, m++) {
		cout << m->name << "  Age:" << m->age << "  ATK:" << m->atk << endl;
	}
}

void main() {
	srand((unsigned int)time(NULL));//调用系统时间,生产随机数
	man m[5];
	int len = sizeof(m) / sizeof(m[0]);
	def(&m[0], len);
	cout << "排序前" << endl;
	print(&m[0], len);
	bubblesort(&m[0], len);
	cout << endl << "排序后" << endl;
	print(&m[0], len);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/569313.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存