数组名作为函数参数

#include <stdio.h>
//数组名做参数,会改变数组本身的值
void printA(int a[],int n){
    printf("%d\n", sizeof(a));//4 传入的是首地址
    for (int i = 0; i < n; ++i) {
        printf("%d ",a[i]);
    }
    printf("\n");
}
void printB(int b[3][4]){
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 4; ++j) {
            printf("%d   ",a[i][j]);
        }
    }
    printf("\n");
}
//指针做参数,会改变数组本身的值
void printAP(int *p, int n){
    printf("%d\n", sizeof(p));  //4 传入的是首地址
    int *pLength =p+n;
    for (; p <pLength ; ++p) {
        printf("%d   ",*p);
    }
    printf("\n");

}
void printBP(int(*p)[4], int n){   //int[4]类型的指针p
    printf("%d\n", sizeof(p));
    int *pInt = *p;  //p是行指针,*p是第0行第一列的列指针
    int *pLength =pInt+n;
    //因为二维数组在内存中以一维数组的形式存储,所以可以以一维指针的形式打印
    for (; pInt <pLength ; ++pInt) {
        printf("%d   ",*pInt);
    }
    printf("\n");
}
void main(){
    int a[5]={1,2,3,4,5};
    printA(a,5);
    printAP(a,5);
    int b[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12};
    printB(b);
    printBP(b,12);
}

 

posted @ 2019-08-06 10:42  Coding_Changes_LIfe  阅读(452)  评论(0)    收藏  举报