【C/C++学院】(3)二维数组/二分查找法/指针/模块注射

1.二维数组

二维数组可以当做一个一维数组, 每一个元素又是一个一维数组。

#include <stdio.h>
#include <stdlib.h>
void main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            printf("%d,%d,%d,%x,%x   ", a[i][j], *(a[i] + j), *(&a[i][j]), &a[i][j],a[i]+j);
        }
        printf("\n");
    }
    //a[x],代表第x行第一个元素首地址 ,某一行这个数组的手地址
    printf("\n\na[0]=%x,a[1]=%x,a[2]=%x", a[0], a[1], a[2]);
    system("pause");
}  

2.二分查找法

while/for 两种方式实现

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


void ShowArray(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d,", a[i]);
	}
	printf("\n");
}

void PaiXu(int a[], int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - 1 - i; j++)
		{
			if (a[j]>a[j+1])
			{
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
}
//num为所要查找的数据,返回数组下标
int SearchFor(int a[], int n, int num)
{
	for (int tou = 0, wei = n - 1, zhong = (tou + wei) / 2; tou <= wei; zhong=(tou+wei)/2)
	{
		printf("\n开始查找%d,%d,%d", tou, wei, zhong);
		if (a[zhong] == num)
		{
			return zhong;
		}
		else   if (a[zhong]>num)
		{
			wei = zhong - 1;
		}
		else
		{
			tou = zhong + 1;
		}
	}
}

int SearchWhile(int a[], int n, int num)
{
	int tou = 0;
	int wei = n - 1;
	int zhong = (tou + wei) / 2;
	while (tou <= wei)
	{
		printf("\n开始查找%d,%d,%d", tou, wei, zhong);
		if (a[zhong] == num)
		{
			return zhong;
		}
		else   if (a[zhong]>num)
		{
			wei = zhong - 1;
			zhong = (tou + wei) / 2;
		}
		else
		{
			tou = zhong + 1;
			zhong = (tou + wei) / 2;
		}
	}
}

void main()
{
	int a[50] = { 0 };
	time_t ts;
	srand((unsigned int)time(&ts));//随机数种子
	for (int i = 0; i < 50; i++)
	{
		a[i] = rand() % 100;
		//printf("%d,", a[i]);
	}
	
	PaiXu(a, 50);
	ShowArray(a, 50);

	int num;
	printf("plesae input your find num:");
	scanf("%d", &num);	//扫描数据接收输入
	//int ret = SearchFor(a, 50, num);
	int ret = SearchWhile(a, 50, num);
	if (ret == -1)
	{
		printf("not find\n");
	}
	else
	{
		printf("find [%d]\n", a[ret]);
	}
	system("pause");
}

技术分享


3.指针、函数指针

普通指针,变量地址,  函数指针, 函数地址。

void(*p1)() = (void(*)())0x2a1118;//函数地址0x2a1118
p1();

#include <stdio.h>
#include <stdlib.h>

int add(int a, int b)
{
	return a + b;
}

void main()
{
	int n = 100;
	int *p = &n;
	*p = 50;
	printf("%d\n", n);

	int(*pf)(int, int);//函数指针
	pf = add;
	printf("%d\n", pf(3, 5));

	system("pause");
}

二级指针;

#include <stdio.h>
#include <stdlib.h>
char a = 'A';
char b = 'B';
char c = 'C';
void change(char **pp)
{
    *pp = &b;
    //**pp = 'D';
}
void main()
{
    char *p = &a;
    printf("我的等级为:[%c]\n", *p);
    change(&p);
    printf("我的等级为:[%c]\n", *p);
    system("pause");
} 


4.模块注射

DllInject.exe 工具

_declspec(dllexport) void go()//导出为dll模块;

靶子程序;

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

char a = 'A';
char b = 'B';
char c = 'C';

void main()
{
	char *p = NULL;
	p = &a;
	printf("p=%x,&a=%x,&b=%x,&c=%x\n", p, &a, &b, &c);
	while (1)
	{
		printf("我的等级为:[%c]\n", *p);
		Sleep(10000);//10s
	}
}

外挂模块;

_declspec(dllexport) void go()//导出为dll模块;
{
	char **pp = (char **)0x288000;//得到p的地址
	*pp = (char *)0x288002;//等级C的地址
}

技术分享



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。