指针数组

指针数组就是元素类型为指针的数组,即带 * 的类型,因此指针数组一个元素占 4 字节

1
2
3
4
5
6
7
char* arr[5] = {0};  //占4*5=20字节

arr[0] = (char*)1; //int强制类型转换成char*
arr[1] = (char*)2;
arr[2] = (char*)3;
arr[3] = (char*)4;
arr[4] = (char*)5;

最常用的指针数组:

1
2
3
4
5
6
char* p1 = "if";			
char* p2 = "for";
char* p3 = "while";
char* p4 = "switch";

char* keyword[] = {p1,p2,p3,p4};

等价于:

1
2
3
4
5
6
char* keyword[] = {			
"if",
"for",
"while",
"switch"
};

就是存的地址嘛,同理可以知道这个 keyword 的大小为 16

结构体指针

探测结构体指针的特性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Arg {		
int a;
int b;
int c;
};
int main(int argc, char* argv[]) {
//带*宽度 ++ -- +整数 -整数 相减 比较大小
Arg* pArg;
Arg* pArg2;

pArg = (Arg*)100;

pArg++; //112,带*类型加减要看减去一个*后的大小,Arg三个int大小12
pArg = pArg + 5; //112+12*5=172

pArg = (Arg*)100;
pArg2 = (Arg*)20;
int x = pArg - pArg2;
printf("%d\n", x); //(100-20)/12 = 6

printf("%d\n", pArg);
return 0;
}

通过结构体指针读取、修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Arg {		
int a;
int b;
int c;
};
int main(int argc, char* argv[]) {
Arg s;
s.a = 10;
s.b = 20;
s.c = 30;

Arg* px = &s;

printf("%d %d %d\n", px->a, px->b, px->c); //结构体指针操作不能用.要用->

px->a = 100;
px->b = 200;
px->c = 300;

return 0;
}

课后作业

  1. 创建一个 int* arr[5] 数组,并为数组赋值(使用&)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #include "stdafx.h"
    int main(int argc,char* argv[]) {
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int* arr[5] = {0};
    arr[0] = &a;
    arr[1] = &b;
    arr[2] = &c;
    arr[3] = &d;
    arr[4] = &e;
    return 0;
    }
  2. 创建一个字符指针数组,存储所有的 C 的关键词(查资料找),并全部打印出来

    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
    #include "stdafx.h"
    int main(int argc,char* argv[]) {
    char* keyword[] = {
    "auto",
    "break",
    "case",
    "char",
    "const",
    "continue",
    "default",
    "do",
    "double",
    "else",
    "enum",
    "extern",
    "float",
    "for",
    "goto",
    "if",
    "int",
    "long",
    "register",
    "return",
    "short",
    "signed",
    "sizeof",
    "static",
    "struct",
    "switch",
    "typedef",
    "union",
    "unsigned",
    "void",
    "volatile",
    "while"
    };
    for (int i = 0; i <32; i++) {
    printf("%s\n", *(keyword + i));
    }
    return 0;
    }
  3. 查找这些数据中,有几个 id=1 level=8 的结构体信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,					
    0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
    0x00,0x33,0x01,0x00,0x00,0x08,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
    0x00,0x00,0x64,0x01,0x00,0x00,0x00,0x08,0x00,0x00,
    0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
    0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
    0x00,0x02,0x57,0x4F,0x57,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

    结构体定义如下:

    1
    2
    3
    4
    typedef struct TagPlayer {		
    int id;
    int level;
    }Player;
    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
    #include "stdafx.h"

    char data[100] = {
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
    0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
    0x00,0x33,0x01,0x00,0x00,0x08,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
    0x00,0x00,0x64,0x01,0x00,0x00,0x00,0x08,0x00,0x00,
    0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
    0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
    0x00,0x02,0x57,0x4F,0x57,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
    };

    typedef struct TagPlayer {
    int id;
    int level;
    } Player;

    int func(int id, int level) {
    int cnt = 0;
    for (int i = 0; i < 100 - 7; i++) {
    Player* pdata = (Player*)(data + i);
    if (pdata->id == id && pdata->level == level) cnt++;
    }
    return cnt;
    }

    int main(int argc,char* argv[]) {
    int num = func(1, 8);
    printf("%d\n", num);
    return 0;
    }