指针3

字符数组与字符串

1
2
3
4
5
6
7
8
9
10
int main(int argc, char* argv[])
{
char arr[6] = {'A','B','C','D','E','F'};

char names[] = "ABCDE";

getchar();
return 0;

}

image-20231110093915024

编译器在字符串的结尾添加\0作为结束标记

image-20231110094119215

常见字符串操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//返回字符的长度
int strlen(char* s)
{
int ret=0;
while(*s!=0)
{
ret++;
s++;
}
return ret;
}
//交换两个字符
char* strcpy (char* dest, char* src)
{
char* ret=dest;
while((*dest++)=(*src++));
return ret;
}
//拼接两个字符
char* strcat (char* dest, char* src)
{
char* ret=dest;
while(*dest!=0)
{
dest++;
}
while((*dest++)=(*src++));
return ret;
}
//比较两个字符是否相等
int strcmp ( char* s1, char* s2)
{
while(*s1 != '\0' && *s2 != '\0')
{
if(*s1!=*s2)
{
return 1;
}
s1++;
s2++;
}
if(*s1 == '\0' && *s2 == '\0')
{
return 0;
}
else
{
return 1;
}

}

int main(int argc, char* argv[])
{
char arr[]="china";
char arr1[]="xyz";


printf("%s",strcat(arr,arr1));

getchar();
return 0;

}

寻址练习

查找char类型数据里为0x64的地址并打印出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "stdafx.h"

char arr[90] = {
0x8d, 0x94, 0x31, 0xa6, 0xfa, 0x2c, 0xd1, 0xe0, 0x57, 0x84,
0xed, 0xbb, 0x41, 0x6f, 0xe5, 0x67, 0xe8, 0x82, 0xb6, 0x2e,
0x7a, 0x55, 0xcb, 0x76, 0x3d, 0xd9, 0x8c, 0xae, 0xbc, 0x4b,
0x52, 0xf4, 0x70, 0xea, 0x6d, 0x3e, 0x9b, 0x6b, 0x97, 0x10,
0x4f, 0x8b, 0x5e, 0x26, 0xce, 0x9c, 0x16, 0xc5, 0x85, 0x90,
0x2f, 0x1c, 0xd7, 0xa5, 0x28, 0x62, 0x1a, 0xf1, 0x14, 0x7e,
0xb0, 0xfd, 0xe7, 0xac, 0x98, 0x9e, 0xda, 0x75, 0xb2, 0xec,
0xc3, 0xd3, 0x72, 0x9d, 0x46, 0xf9, 0xcf, 0xdb, 0x13, 0x7b,
0x65, 0x3c, 0x23, 0xf2, 0x60, 0x2b, 0xab, 0xa2, 0x83, 0x64
};

void fun()
{

for(int i=0;i<90;i++)
{
if(arr[i]==0x64)
{
printf("%x\n",&arr[i]);
}
}

}

int main(int argc, char* argv[])
{

fun();
getchar();
return 0;

}

查找int类型数据里为0x64的地址并打印出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

char arr[100] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
0x00,0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00

};

void fun()
{
char* p=arr;


for(int i=0;i<97;i++)
{
if(*(int*)(p+i)==100)//先以一字节的方式查询所有数据,再转化为四字节指针来满足宽度需求
{
printf("%x\n",(p+i));
}


}

}

int main(int argc, char* argv[])
{

fun();
getchar();
return 0;

}

image-20231110094410162