#include <stdio.h> void pop(int(*ptr)[4]); //函数原型,定义一个int类型的包含4个元素的指针数组 void pop2(int(*ptr)[4],int n); //函数原型,定义一个int类型的包含4个元素的指针数组,和一个int类型 int main(void) { int multi[3][4]= { { 1,2,3,4 } , { 5,6,7,8 } , { 9,10,11,12 } } ; int(*ptr)[4],count ; ptr=multi ; //循环2次,依次打印出二维数组的每个元素,因为不循环2次,只能打印出来1,2,3,4,循环2次后依次取出 for(count=0;count<3;count++)后 面 的 5 , 6 , 7 , 8 和 9 , 10 , 11 , 12 pop(ptr++); //有些函数调用时是直接pop();的,在本例中这样写是错误的。记住,在定义函数时如果没有传递给函数的必要的话,应该是void puts("\n-----------------------------"); getchar(); //按回车后打印第二种方法的显示结果 pop2(multi,3); getch(); }
void pop(int(*ptr)[4]) { int*p,count ; p=(int*)ptr ; //强制转换为int类型 //循环3次,依次打印出二维数组的每个元素 for(count=0;count<4;count++)printf("\n%d",*p++); //指针赋给指针时都是地址赋给地址,打印时必须加上*号,否则打印出来的是地质而不是数据 }
//第二种方法 void pop2(int(*ptr)[4],int n) { int*p,count ; p=(int*)ptr ; //强制转换为int类型 //循环11次 for(count=0;count<(4*n);count++)printf("\n%d",*p++); } 
|