吾爱汇编

 找回密码
 立即注册

QQ登录

绑定QQ避免忘记帐号

查看: 3762|回复: 57

[C/C++] 学习c\c++时的练习

  [复制链接]
白云点缀的蓝 发表于 2021-6-23 00:22 | 显示全部楼层 |阅读模式

练习2.jpg

练习.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 ;//防止报错
}

评分

参与人数 6HB +9 收起 理由
消逝的过去 + 2
后学真 + 1
风里去 + 1 [吾爱汇编论坛52HB.COM]-软件反汇编逆向分析,软件安全必不可少!
agan8888 + 1
zxjzzh + 2 [吾爱汇编论坛52HB.COM]-学破解防破解,知进攻懂防守!
pmm018 + 2

查看全部评分

吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
XxrIL25901 发表于 2022-3-1 01:16 | 显示全部楼层
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
eklSC40 发表于 2022-3-27 19:24 | 显示全部楼层

感谢楼主
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
KUp9281 发表于 2022-4-1 02:35 | 显示全部楼层
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
OkPFH82 发表于 2022-4-2 08:15 | 显示全部楼层

回复,顶帖,赚币,谢谢楼主
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
wAYxfpIgc 发表于 2022-4-2 08:15 | 显示全部楼层
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
dUMBfkIv 发表于 2022-4-27 04:16 | 显示全部楼层

谢谢分享
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
EQJjHBgCr 发表于 2022-4-27 04:32 | 显示全部楼层

感谢楼主
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
nNDtsS316 发表于 2022-4-27 07:37 | 显示全部楼层

不可多得,支持楼主
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
CBJ6245 发表于 2022-4-29 18:41 | 显示全部楼层

感谢楼主
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

警告:本站严惩灌水回复,尊重自己从尊重他人开始!

1层
2层
3层
4层
5层
6层
7层
8层
9层
10层

免责声明

吾爱汇编(www.52hb.com)所讨论的技术及相关工具仅限用于研究学习,皆在提高软件产品的安全性,严禁用于不良动机。任何个人、团体、组织不得将其用于非法目的,否则,一切后果自行承担。吾爱汇编不承担任何因为技术滥用所产生的连带责任。吾爱汇编内容源于网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除。如有侵权请邮件或微信与我们联系处理。

站长邮箱:SharkHeng@sina.com
站长QQ:1140549900


QQ|RSS|手机版|小黑屋|帮助|吾爱汇编 ( 京公网安备11011502005403号 , 京ICP备20003498号-6 )|网站地图

Powered by Discuz!

吾爱汇编 www.52hb.com

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