吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2018|回复: 14
收起左侧

[学习记录] 学习c\c++时的练习

  [复制链接]
白云点缀的蓝 发表于 2021-6-23 00:21
练习.jpg

练习2.jpg

[C] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#define RETURN 0;
//1 野指针
int main1() {
	int* p;
	p = 100;
	printf("%d\n", p);//100
	//printf("%d\n", *p);//访问权限冲突
}
//2 空指针
int main2() {
	main1();
	int* p = NULL;
	int* p1 = 0x00000000;
	//printf("%d\n",*p);//nullptr
	//printf("%d\n", *p1);//nullptr
	printf("%d\n", p);//0
	printf("%d\n", p1);//0
	printf("%X\n", p);//0
	printf("%X\n", p1);//0
	return RETURN;
}
//3 万能指针
int main3() {
	int num = 100;
	void* p = #
	printf("%d\n",p);//9435172
	printf("%d\n",(int *)p);//9435172
	printf("%x\n",p);//8ff824
	printf("%x\n", (int*)p);//8ff824
//	printf("\n", *p);//无法编译通过
	printf("%d\n",*(int*)p); //100
	char s = 'A';
	void* ps = s;
	//printf("%x\n",*(char * )ps);//异常
	printf("%x\n", (char*)ps);//41
	printf("%d\n",(char * )ps);//65
	unsigned long num1 =12345678 ;
	void* p2 = &num1;
	printf("%d\n", *(int*)p2);//12345678
	printf("%d\n",*(unsigned long *)p2);//12345678
	return RETURN;

}
//4  const修饰的指针变量
void main4() {
	int num = 100;
	const int* p = #
	//*p = 20;//无法编译通过
	p = NULL;
	printf("%x\n", p);//0
	int * const p1 = #
	*p1 = 20;
	printf("%d\n",*p1);//20
	//p1 = 200;//无法编译通过
	const int* const p2 = #
	//*p2 = 20;//无法编译通过
	//p2= 100;//无法编译通过
	return ;
}
//5 多级指针 
int main5() {
	int a = 666;
	printf("%X\n", &a);//EFFE30
	int* p = &a;
	printf("%X\n", p);//EFFE30
	int** p1 = &p;
	printf("%X\n", p1);//EFFE24
	printf("%X\n", *p1);//EFFE30
	printf("%X\n", ** p1);//29A=666
	return RETURN;
}
//6  指针结合数组
int main() {
	char dis[] = "hello";
	char res[] = "world";
	int lengthres= sizeof(res) / sizeof(res[0]);
	int lengthres2 = sizeof(dis) / sizeof(dis[0]);
	extern char* my_strcopy(char* res, char* dis, int lengthres, int lengthres2);
	extern char* my_strcopy2(char* res, char* dis,int lengthres,int lengthres2);
	char* p = my_strcopy2(res, dis, lengthres, lengthres2);
	char *p2= my_strcopy2(res, dis, lengthres, lengthres2);
	printf("%X\n", p);//1658A60
	printf("%X\n", p2);//1658A60
	printf("%s\n", p);//hellworld
	printf("%s\n", p2);//hellworld
	free(p);//释放内存
	free(p2);//释放内存
	printf("%X\n", p);//1658A60
	printf("%X\n", p2);//1658A60
	printf("%s\n", p);//葺葺葺葺葺葺葺葺葺葺
	printf("%s\n", p2);//葺葺葺葺葺葺葺葺葺葺
	return RETURN;
}
char * my_strcopy(char * res,char * dis, int lengthres, int lengthres2){
	printf("%d%d\n", lengthres, lengthres2);//6//6
	printf("%X\n", res);
	printf("%d\n", sizeof(res) / sizeof(res[0]));//4
	printf("%d\n", sizeof(dis) / sizeof(dis[0]));//4
	printf("%X\n", dis);
	int strlength=strlen(dis);
	int strlength2 = strlen(res);
	char *p= malloc(strlength + strlength2 + 1);
	printf("%d\n", strlength);
	int num1 = 0;
	int num2= 0;
	for (size_t i = 0; i < strlength; i++)
	{
		p[i] = dis[i];
		num1 = i;
	}
	for (int i = 0; i < strlength2; i++)
	{		
			p[num1 + i] = res[i];
			num2 = i;
			if (i == 4) {
				p[num1 + i + 1] =  '\0';
			}
	}
	//p[num2+num1+1] = '\0';
	printf("%s\n", p);//hellworld
	return p;
}
char* my_strcopy2(char* res, char* dis, int lengthres, int lengthres2) {
	printf("%d%d\n", lengthres, lengthres2);//6//6
	printf("%X\n", res);
	printf("%d\n", sizeof(res) / sizeof(res[0]));//4
	printf("%d\n", sizeof(dis) / sizeof(dis[0]));//4
	printf("%X\n", dis);
	int strlength = strlen(dis);
	int strlength2 = strlen(res);
	char* p = malloc(strlength + strlength2 + 1);
	printf("%d\n", strlength);
	int num1 = 0;
	int num2 = 0;
	for (size_t i = 0; i < strlength; i++)
	{
		//p[i] = dis[i];
		*(p + i) = *(dis + i);
		num1 = i;
	}
	for (int i = 0; i < strlength2; i++)
	{
		//p[num1 + i] = res[i];
		*(p + num1 + i) = *(res + i);
		num2 = i;
		if (i == 4) {
			p[num1 + i + 1] = '\0';
		}
	}
	//p[num2+num1+1] = '\0';
	printf("%s\n", p);//hellworld
	return p;
}
//7 指针运算
int main9() {
	int* arr[8];
	int a[8];
	for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
	{
		srand(time(0));
		*(a + i) = rand();
		arr[i] = &a[i];
		//Sleep(1000);
	}
	for (int  i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
	{
		printf("%d\n",*arr[i]);
	/*
			3208
			3212
			3215
			3218
			3221
			3225
			3228
			3231*/
	}
	printf("%X\n", arr[7]);//8FF96C
	printf("%X\n", arr[0]);//8FF954
	printf("%X\n", arr[7] - arr[0]);//7,中间相差7个元素
	return RETURN;
}
//8 []并不是数组的专属
int main10() {
	int num[100];
	int length = sizeof(num) / sizeof(num[0]);
	for (int i = 0; i < sizeof(num) / sizeof(num[0]); i++)
	{	
		num[i] = i;//100个值
	}
	for (int i = 0; i < sizeof(num)/sizeof(num[0]); i++)
	{
		printf("%d\n", num[i]);//100个值

	}
	for (int i = 0; i < length; i++)
	{
		printf("%X\n", (num + i));//num[i]里的值的地址
	}
	for (int i = 0; i < length; i++)
	{
		printf("%d\n", *(num + i));//num[i]的值
	}
	int num1 = 100;
	int* p = &num1;
	*p = 200;
	printf("num1=%d\n", num1);
	printf("&num1=%X\n",&num1);
	printf("p[0]=%X\n", p[0]);
	//p[0] = 500;
	p[0] = 500;
	printf(" p[0] == 500 ? 1:2=%d\n", p[0] == 500 ? 1:2);
		/*num1=200
		&num1=25F7E8
		p[0]=C8
		 p[0] == 500 ? 1:2=1*/
	return RETURN;
}
//9 指针数组
int main6() {
	int* a[3];
	int b = 100;
	int c = 200;
	int d = 300;
	a[1] = &b;
	a[0] = &c;
	a[2] = &d;
	//a[3] = &a;//数组越界
	int length = sizeof(a) / sizeof(a[0]);
	for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
	{
		printf("%X\n",a[i]);
			/*
			EFFBD4 &c
			EFFBE0 &b
			EFFBC8 &d
			*/
	}
	for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
	{
		printf("%d\n", *a[i]);
	/*		200
			100
			300*/
	}
	for (int i = 0; i <length ; i++)
	{
		//printf("%d\n", a);//&#8237;5CFC50&#8236;
		printf("**(a + i)%d\n", **(a + i));//200 100 300
		printf(" *(a + i)%X\n", *(a + i));//c b d的地址
		printf("(a + i)%X\n", (a + i));//a[i]数据的地址
		printf("%X\n", &a[i]);//a[i]数据的地址

	}
	return RETURN;
}
//10 指针作为函数的形参,并且改变实参的值
swapNum(int* num11, int* num22) {
	int tmp = *num11;
	*num11 = *num22;
	*num22 = tmp;
}
int main7() {
	int num1 = 100;
	int num2 = 200;
	extern swapNum(int* num11, int* num22);
	swapNum(&num1, &num2);
	printf("%d\n", num1);//200
	printf("%d\n", num2);//100
	return RETURN;
}

//11  数组作为函数的形参
int main11() {
	extern int* myPrint(int a[], int b[]);
	extern int* myPrint1(int a[], int b[],int lengthA,int lengthB);
	int a[] = { 1,2,3,4,5,6,7,8,9,10 };
	int b[] = { 11,22,33,44,55,66,77,88,99,00 };
	myPrint1(a, b,sizeof(a)/sizeof(a[0]), sizeof(b) / sizeof(b[0]));
}
int* myPrint(int a[], int b[]) {
	int lengthA = sizeof(a) / sizeof(a[0]);
	int lengthB = sizeof(b) / sizeof(b[0]);
	printf("%d\n", lengthA);
	printf("%d\n", lengthB);
	for (int i = 0; i < lengthA; i++)
	{	
		printf("%d\n", a[i]);
	}
	for (int i = 0; i < lengthB; i++)
	{
		printf("%d\n", b[i]);
	}
	return NULL;
}
int* myPrint1(int a[], int b[],int lengthA, int lengthB) {
	/*int lengthA = sizeof(a) / sizeof(a[0]);
	int lengthB = sizeof(b) / sizeof(b[0]);*/
	printf("%d\n", lengthA);
	printf("%d\n", lengthB);
	for (int i = 0; i < lengthA; i++)
	{
		printf("%d\n", a[i]);
	}
	for (int i = 0; i < lengthB; i++)
	{
		printf("%d\n", b[i]);
	}
	/*10
		10
		1
		2
		3
		4
		5
		6
		7
		8
		9
		10
		11
		22
		33
		44
		55
		66
		77
		88
		99
		0*/
	return NULL;
}
//12  指针作为函数的返回值
int main8() {
	int num = 100;
	extern int* add(int* num11, int* num22);
	int num1 = 200;
	printf("%X\n", add(&num, &num1));//97FB08
	printf("%d\n", *(add(&num, &num1)));//300
	return RETURN;
}
int * add(int* num11, int* num22) {
 	int Num= (*num11 + *num22);
	return &Num;
	//return NULL ;//防止报错
}

免费评分

参与人数 5吾爱币 +4 热心值 +5 收起 理由
a151652 + 1 + 1 用心讨论,共获提升!
kentish + 1 + 1 用心讨论,共获提升!
Luckyu920 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Didor + 1 + 1 谢谢@Thanks!
bingshuir + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
tlf 发表于 2021-6-23 05:21
提示: 作者被禁止或删除 内容自动屏蔽
Didor 发表于 2021-6-23 08:52
andu 发表于 2021-6-23 09:02
EliVenom 发表于 2021-6-23 09:21
学C的指针时真的脑壳疼,但真的很强,加油
zz0eyu 发表于 2021-6-23 11:06
这个背景真心不错
最近也在学习C++
alicc 发表于 2021-6-23 11:38
大佬 你看的那个教程  我也学习
kentish 发表于 2021-6-23 12:01
感谢分享 能够重温一次
 楼主| 白云点缀的蓝 发表于 2021-6-24 12:08
alicc 发表于 2021-6-23 11:38
大佬 你看的那个教程  我也学习

链接:https://pan.baidu.com/s/1mIQ-RydkWQFqmF9kSJ54dg 提取码:1321 复制这段内容后打开百度网盘手机App,操作更方便哦
alicc 发表于 2021-6-24 12:56
bsjasdq 发表于 2021-6-24 12:08
链接:https://pan.baidu.com/s/1mIQ-RydkWQFqmF9kSJ54dg 提取码:1321 复制这段内容后打开百度网盘手机App ...

谢谢               
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 00:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表