子函数通过一级指针访问二维数组

#include <stdio.h>
#define COLS 10
#define ROWS 6
char erwei[6][10]={{1,2,3,4,5,6,7,8,9,10},
                {10,9,8,7,6,5,4,3,2,1},
                {1,2,3,4,5,6,7,8,9,10},
                {10,9,8,7,6,5,4,3,2,1},
                {1,2,3,4,5,6,7,8,9,10},
                {10,9,8,7,6,5,4,3,2,1}            
                };
                
test (char *image){
    int i,j=0;
    for(i=0;i<COLS*ROWS;i++){
        printf("image[%d][%d]%d
",i,j,*image); 
        image++;
    }    
}

test2 (char *image){
    int i,j=0;
    for(i=0;i<ROWS;i++){
        for(j=0;j<COLS;++j){
            printf("image[%d][%d]%d
",i,j,image[i*10+j]);     
        }
    }    
}

void main()
{
    test((char *)erwei);   // test(&erwei);
    printf("


");
    test2((char *)erwei);  // test2(&erwei);
    return 0;
}


    
原文地址:https://www.cnblogs.com/ligei/p/12183085.html