指向數組的指針專題

1、

#include <stdio.h>
int main()
{
	int a[]={1,2,3,4,5,6},*p;
	p=a;
	printf("%d,%d\n",*p,*(p+3));
	*(p+3)+=2;
	printf("%d,%d\n",*p,*(p+3));
	for(int i=0;i<6;i++){
		printf("%d ",*(p+i));
	}
	return 0; 
}

2、

    int a[2][5]={{2,3,4,5,6},{7,8,9,10,11}};
    int *ptr = (int*)(&a+1);
    printf("%d\n",&a);
    printf("%d\n",&a+1);
    printf("%d\n",*(&a+1));
    printf("%d\n",*(ptr-3));
    printf("%d\n",(ptr-3));    
    return 0; 

3.

#include <stdio.h>
int main(){
    int n[][3] = {10,20,30,40,50,60}; //n={{10,20,30},{40,50,60}}
    int (*p)[3]; // p = (int(*)[3]) 0x1
    p=n;//  p = (int(*)[3]) 0x23fe30
	printf("%d %d %d\n",p[0][0],*(p[0]+1),(*p)[2]);	// 10 20  30
	return 0; 
}

// *p = {10,20,30}
// p[0] = {10,20,30}
// *p[0] = 10
// (*p)[0] = 10
// p[0]+1 = 0x23fe34
// *p[0]+1 = 11

4、

#include <stdio.h>

int main(){
    int a[5] = {1,3,5,7,9};
	int *p = (int *)(&a+1);
	printf("%d,%d",*(a+1),*(p-1));	// 3  9 
	return 0; 
}
// &a = (int (*)[5]) 0x23fe30
// &a+1 = &a+5*4 = (int (*)[5]) 0x23fe44 (十六進制)
// p =  &a+1
// *p=0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章