#include <stdio.h>
#include <stdlib.h>
char * get(int i){
static char str1[]="b ";
static char str2[]="a ";
static char str3[]="ptr1";
static char str4[]="ptr2";
switch(i){
case 0:
return str4;
case 1:
return str3;
case 2:
return str1;
case 3:
return str2;
default:
return "known";}
}
void getaddress(int ptr, int * ptr3){
for (int i=0;i<4;i++){
ptr3=(int*)(ptr+4*i);
printf("%s =%p, * in hex =%08X, * in dec =%08d\n",get(i), ptr3, *ptr3, *ptr3);
}
printf("\n");
}
int main(void){
int i=0,a,b;
int *ptr1, *ptr2,*ptr3;
int ptr = (int)&ptr2;
printf("&i=%p\n",&i);
printf("&a=%p\n",&a);
printf("&b=%p\n",&b);
printf("&ptr1=%p\n",&ptr1);
printf("&ptr2=%p\n",&ptr2);
getaddress(ptr, ptr3);
a=5,b=10;
printf("a=5,b=10;\n");
getaddress(ptr, ptr3);
ptr1 = &a; ptr2 = &b;
printf("ptr1 = &a; ptr2 = &b;\n");
getaddress(ptr, ptr3);
*ptr1=7; *ptr2=32; //a=7, b=32
printf("*ptr1=7; *ptr2=32;\n");
getaddress(ptr, ptr3);
a=17; //a=17, b=32
printf("a=17;\n");
getaddress(ptr, ptr3);
ptr1=ptr2; //ptr1=&b
printf("ptr1=ptr2;\n");
getaddress(ptr, ptr3);
*ptr1=9; //b=9, a=17
printf("*ptr1=9;\n");
getaddress(ptr, ptr3);
ptr1=&a; //ptr1=&a
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
a=64; //a=64, b=9, ptr1=&a, ptr2=&b
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
*ptr2=*ptr1+5; // b=a+5=64+5=69
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
ptr2=&a; //ptr2=&a, ptr1=&a
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
printf("a=%2d, b=%2d, *ptr1=%2d, *ptr2=%2d\n", a, b, *ptr1, *ptr2);
printf(" ptr1=%p, ptr2=%p\n", ptr1, ptr2);
printf(" ptr1=%p, &*ptr1=%p\n", ptr1, &*ptr1);
ptr1=(int*)0x28FEA0;
printf(" ptr1=%p, *ptr1=%d\n", ptr1, *ptr1);
system("pause");
return 0;
}
結果
&i=0028FEB4
&a=0028FEB0
&b=0028FEAC
&ptr1=0028FEA8
&ptr2=0028FEA4
ptr2 =0028FEA4, * in hex =00405000, * in dec =04214784
ptr1 =0028FEA8, * in hex =0028FEC8, * in dec =02686664
b =0028FEAC, * in hex =004021DB, * in dec =04202971
a =0028FEB0, * in hex =00402180, * in dec =04202880
a=5,b=10;
ptr2 =0028FEA4, * in hex =00405000, * in dec =04214784
ptr1 =0028FEA8, * in hex =0028FEC8, * in dec =02686664
b =0028FEAC, * in hex =0000000A, * in dec =00000010
a =0028FEB0, * in hex =00000005, * in dec =00000005
ptr1 = &a; ptr2 = &b;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =0000000A, * in dec =00000010
a =0028FEB0, * in hex =00000005, * in dec =00000005
*ptr1=7; *ptr2=32;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000020, * in dec =00000032
a =0028FEB0, * in hex =00000007, * in dec =00000007
a=17;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000020, * in dec =00000032
a =0028FEB0, * in hex =00000011, * in dec =00000017
ptr1=ptr2;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEAC, * in dec =02686636
b =0028FEAC, * in hex =00000020, * in dec =00000032
a =0028FEB0, * in hex =00000011, * in dec =00000017
*ptr1=9;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEAC, * in dec =02686636
b =0028FEAC, * in hex =00000009, * in dec =00000009
a =0028FEB0, * in hex =00000011, * in dec =00000017
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000009, * in dec =00000009
a =0028FEB0, * in hex =00000011, * in dec =00000017
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000009, * in dec =00000009
a =0028FEB0, * in hex =00000040, * in dec =00000064
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000045, * in dec =00000069
a =0028FEB0, * in hex =00000040, * in dec =00000064
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEB0, * in dec =02686640
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000045, * in dec =00000069
a =0028FEB0, * in hex =00000040, * in dec =00000064
a=64, b=69, *ptr1=64, *ptr2=64
ptr1=0028FEB0, ptr2=0028FEB0
ptr1=0028FEB0, &*ptr1=0028FEB0
ptr1=0028FEA0, *ptr1=48
請按任意鍵繼續 . . .
沒有留言:
張貼留言