C pass int array pointer as parameter into a function

前端 未结 9 1481
借酒劲吻你
借酒劲吻你 2020-12-08 04:39

I want to pass the B int array pointer into func function and be able to change it from there and then view the changes in main function

#include 

        
9条回答
  •  旧时难觅i
    2020-12-08 05:18

    Make use of *(B) instead of *B[0]. Here, *(B+i) implies B[i] and *(B) implies B[0], that is
    *(B+0)=*(B)=B[0].

    #include 
    
    int func(int *B){
        *B = 5;     
        // if you want to modify ith index element in the array just do *(B+i)=
    }
    
    int main(void){
    
        int B[10] = {};
        printf("b[0] = %d\n\n", B[0]);
        func(B);
        printf("b[0] = %d\n\n", B[0]);
        return 0;
    }
    

提交回复
热议问题