指针4

指针数组

与普通的数组没有区别,只不过是int类型的数组存放的是int类型的数据,而指针数组存放的是指针类型的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"


int main(int argc, char* argv[])
{
int* arr[]={0};

int a=10;
int b=20;
int c=30;

arr[0]=&a;//取地址符获得int类型的指针
arr[1]=&b;
arr[2]=&c;

getchar();
return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"


int main(int argc, char* argv[])
{
char* arr[] =
{
"if",
"while",
"for"
};


getchar();
return 0;

}

image-20231110094814759

结构体指针

结构体指针进行加法运算最后的结果类型不变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"

struct arg
{
int a;
int b;
int c;
};

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

arg* parg;

parg=(arg*)100;

parg++;

printf("%d",parg);

getchar();
return 0;

}

image-20231113104300287

进行减法运算结果变为int类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"

struct arg
{
int a;
int b;
int c;
};

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

arg* parg=(arg*)100;
arg* parg1=(arg*)20;

int x=parg-parg1;

printf("%d",x);

getchar();
return 0;

}

操作结构体

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
#include "stdafx.h"

struct arg
{
int a;
int b;
int c;
};

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

arg x;

x.a=10;
x.b=20;
x.c=30;

arg* px=&x;

printf("%d ",px->a);
printf("%d ",px->b);
printf("%d ",px->c);
getchar();
return 0;

}

image-20231113104938269