学习“char*数组 转换成 int*数组 和int* 类型”测试总结

//char*数组 转换成 int*数组 和int* 类型  测试


#include "stdafx.h"


char* keyword[] ={"if","for","while","switch"};
int* intpk[] ={(int*)keyword,(int*)keyword[0],(int*)keyword[1],(int*)keyword[2],(int*)keyword[3]};

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

    char x = 'A';
     char* px = &x;
     char arr[]={'c','h','i','n','a',''};
     int* intkey=(int*)keyword;
     printf("%s ",arr);
     printf("%X %s %s %s %s ",keyword,*(keyword),*(keyword+1),*(keyword+2),*(keyword+3));
     printf("%s ",*(intkey));
    printf("%X %s %s %s %s ",*(intpk),*(intpk+1),*(intpk+2),*(intpk+3),*(intpk+3));

    return 0;
}



--------------------------------------------------------------------------------------

查看汇编编译:

0040B848   mov         byte ptr [ebp-4],41h
0040B84C   lea         eax,[ebp-4]
0040B84F   mov         dword ptr [ebp-8],eax
0040B852   mov         byte ptr [ebp-10h],63h
0040B856   mov         byte ptr [ebp-0Fh],68h
0040B85A   mov         byte ptr [ebp-0Eh],69h
0040B85E   mov         byte ptr [ebp-0Dh],6Eh
0040B862   mov         byte ptr [ebp-0Ch],61h
0040B866   mov         byte ptr [ebp-0Bh],0
0040B86A   mov         dword ptr [ebp-14h],offset keyword (00423428)   //keyword 实际存储位置:423428
0040B871   lea         ecx,[ebp-10h]
0040B874   push        ecx
0040B875   push        offset string "%s " (00420f3c)
0040B87A   call        printf (00401180)
0040B87F   add         esp,8
0040B882   mov         edx,dword ptr [keyword+0Ch (00423434)]
0040B888   push        edx
0040B889   mov         eax,[keyword+8 (00423430)]
0040B88E   push        eax
0040B88F   mov         ecx,dword ptr [keyword+4 (0042342c)]
0040B895   push        ecx
0040B896   mov         edx,dword ptr [keyword (00423428)]
0040B89C   push        edx
0040B89D   push        offset keyword (00423428)

0040B8A2   push        offset string "%s %s %s %s " (00420f9c)
0040B8A7   call        printf (00401180)
0040B8AC   add         esp,18h
0040B8AF   mov         eax,dword ptr [ebp-14h]
0040B8B2   mov         ecx,dword ptr [eax]
0040B8B4   push        ecx
0040B8B5   push        offset string "%s " (00420034)
0040B8BA   call        printf (00401180)
0040B8BF   add         esp,8
0040B8C2   mov         edx,dword ptr [intpk+0Ch (00423444)]
0040B8C8   push        edx
0040B8C9   mov         eax,[intpk+0Ch (00423444)]
0040B8CE   push        eax
0040B8CF   mov         ecx,dword ptr [intpk+8 (00423440)]
0040B8D5   push        ecx
0040B8D6   mov         edx,dword ptr [intpk+4 (0042343c)]
0040B8DC   push        edx
0040B8DD   mov         eax,[intpk (00423438)]
0040B8E2   push        eax

0040B8E3   push        offset string "%X %s %s %s %s " (00420fac)
0040B8E8   call        printf (00401180)
0040B8ED   add         esp,18h
0040B8F0   xor         eax,eax


--------------------------------------------------------------------------------------

打印结果:

china
423428  if      for     while   switch
if
423428  if      for     while   while

--------------------------------------------------------------------------------------

C编译器总结 :

1、各种指针类型 之间可以转换,但需要强制转换—>int* intpk[] ={(int*)keyword,(int*)keyword[0],(int*)keyword[1],(int*)keyword[2],(int*)keyword[3]};

2、指针可以+- 操作,步长为4个字节

3、*在变量的左边为取值(取值类型可以看成是 :例如 int* –* = int,int** – * = int*)

4、字符型数组,可以看成是字符型指针类型char*;但有区别本例没有展示出来。

5、&变量 来赋值 可以不用强制类型

原文地址:https://www.cnblogs.com/killad/p/7087615.html