开发者社区 问答 正文

C语言中数组作为参数传递给函数时,不能使用sizeof,那么该如何传递数组大小呢?

C语言中数组作为参数传递给函数时,不能使用sizeof,那么该如何传递数组大小呢?

展开
收起
游客gaiketk6mpmke 2022-03-29 15:12:07 793 分享 版权
1 条回答
写回答
取消 提交回答
  • 在被调函数中无法使用sizeof,可以在主函数中直接将长度传递进被调函数。

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    //函数预声明
    void printVector(double(*p)[3], int m);//向量的打印
    //主函数
    int main(void)
    {
        int u[3] = { 1, 2, 3};
        printVector(u, sizeof(u)/sizeof(u[0]));
        system("pause");
    }
    //打印
    void printVector(int p[], int m)
    {
        for (int i = 0; i < m; i++)
        {
            printf("%lf  ", p[i]);
        }
    }
    
    2022-03-29 16:47:13
    赞同 展开评论