[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);//‭5CFC50‬
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 ;//防止报错
}