C语言14 指针类型

Asura 2018年01月11日 98次浏览

C语言14 指针类型

指针是C语言中的一种数据类型,

定义带 *类型的变量

char    x;          char*       x;
short   y;          short*      y;
int     z;          int*        z;
float   f;          float*      f;
double  d;          double*     d;
Student st;         Student*    st;

总结:

  1. 任何带有*的变量类型的标准写法:变量类型* 变量名
  2. 任何类型都可以带* 加上* 以后是新的类型,统称为“指针类型”
  3. *可以使任意多个。

指针变量赋值

char* x;        char*** x;          char******* x;
short* ~~
int* ~~
Student* ~~
x = (char*)1;
y = (short*)2;
int******* x;
int******* y;
x = (int*******)4;
y = x;
char x;
x = 1;

指针变量的宽度

char x;     char* x;        char******** x;
short y;    short* y;       short******** y;
~~
x=1;    x=(char*)1;         x=(char********)1;
~~
9:        char* x;
10:       short* y;
11:       int* z;
12:
13:       x = (char*)1;
00401028 C7 45 FC 01 00 00 00 mov         dword ptr [ebp-4],1
14:       y=(short*)2;
0040102F C7 45 F8 02 00 00 00 mov         dword ptr [ebp-8],2
15:       z=(int*)3;
00401036 C7 45 F4 03 00 00 00 mov         dword ptr [ebp-0Ch],3
9:        char******** x;
10:       short******** y;
11:       int******** z;
12:
13:       x = (char********)1;
00401028 C7 45 FC 01 00 00 00 mov         dword ptr [ebp-4],1
14:       y=(short********)2;
0040102F C7 45 F8 02 00 00 00 mov         dword ptr [ebp-8],2
15:       z=(int********)3;
00401036 C7 45 F4 03 00 00 00 mov         dword ptr [ebp-0Ch],3

总结:
指针类型的变量宽度永远是4字节、无论类型是什么 无论有几个*。

指针类型自加和自减(++ / –)

char a;                     char**** a;
short b;                    short**** b;
int c;                      int**** c;
a=100;                      a=(char****)100;
b=100;                      b=(short****)100;
c=100;                      c=(int****)100;
a++;                        a++;
b++;                        b++;
c++;                        c++;
printf("%d %d %d",a,b,c);   printf("%d %d %d",a,b,c);
11:       char* a;
12:       short* b;
13:       int* c;
14:       a=(char*)100;
0040B488 C7 45 FC 64 00 00 00 mov         dword ptr [ebp-4],64h
15:       b=(short*)100;
0040B48F C7 45 F8 64 00 00 00 mov         dword ptr [ebp-8],64h
16:       c=(int*)100;
0040B496 C7 45 F4 64 00 00 00 mov         dword ptr [ebp-0Ch],64h
17:       a++;
0040B49D 8B 45 FC             mov         eax,dword ptr [ebp-4]
0040B4A0 83 C0 01             add         eax,1
0040B4A3 89 45 FC             mov         dword ptr [ebp-4],eax
18:       b++;
0040B4A6 8B 4D F8             mov         ecx,dword ptr [ebp-8]
0040B4A9 83 C1 02             add         ecx,2
0040B4AC 89 4D F8             mov         dword ptr [ebp-8],ecx
19:       c++;
0040B4AF 8B 55 F4             mov         edx,dword ptr [ebp-0Ch]
0040B4B2 83 C2 04             add         edx,4
0040B4B5 89 55 F4             mov         dword ptr [ebp-0Ch],edx
比如
int* c =1; c++; 去掉* 是int   4个字节 就+4;
char* c=1; c++; 去掉* 是char  1个字节 就+1
char** c=1; c++; 去掉* 是char* 指针类型是4个字节 就+4
指针类型变量+N=指针类型变量+N*(去掉一个*后类型的宽度)
指针类型变量-N=指针类型变量-N*(去掉一个*后类型的宽度)

总结、

  1. 不带*变量的变量,++或者–都是加1 或者减1
  2. *类型的变量m,++或者– 新增(减少)的数量是去掉一个*后变量的宽度

指针类型的比较

char**** a;
char**** b;
a = (char****)200;
b = (char****)100;
if(a>b)
{
    printf("1");
}
else
{
    printf("2");
}
12:       char**** a;
13:       char**** b;
14:       a = (char****)200;
0040B488 C7 45 FC C8 00 00 00 mov         dword ptr [ebp-4],0C8h
15:       b = (char****)100;
0040B48F C7 45 F8 64 00 00 00 mov         dword ptr [ebp-8],64h
16:       if(a>b)
0040B496 8B 45 FC             mov         eax,dword ptr [ebp-4]
0040B499 3B 45 F8             cmp         eax,dword ptr [ebp-8]
0040B49C 76 0F                jbe         main+3Dh (0040b4ad)
17:       {
18:           printf("1");
0040B49E 68 1C F0 41 00       push        offset string "1" (0041f01c)
0040B4A3 E8 A8 02 00 00       call        printf (0040b750)
0040B4A8 83 C4 04             add         esp,4
19:       }
20:       else
0040B4AB EB 0D                jmp         main+4Ah (0040b4ba)
21:       {
22:           printf("2");
0040B4AD 68 50 FE 41 00       push        offset string "2" (0041fe50)
0040B4B2 E8 99 02 00 00       call        printf (0040b750)
0040B4B7 83 C4 04             add         esp,4
//这里编译器使用了jbe指令 jbe指令是用来比较无符号数的,也就说明指针类型是无符号数

总结
指针类型可以做大小比较