c - 发送一个二维数组来计算总数并在函数中显示结果

标签 c

我想找到数组的大小,由另一个函数分隔

我尝试发送信息。但它没有正确显示结果

int main()
{
int amount[2];
int price[2][4];
int total[4];

getData(amount,price,total);
getSizeOfArray(amount,price,total);

}
void getData(int amount[2],int price[2][4],int total[2])
{
for(int i=0;i<2;i++)
{
    printf("+++Department %d+++\n\n",i+1);
    for(int j=0;j<4;j++)
    {
        printf("Costomer %d\n",j+1);
        printf("Enter amount : ");
        scanf("%d",&amount[i]);
        printf("Enter price : ");
        scanf("%d",&price[i][j]);

        total[j]=amount[i]*price[i][j];
    }
    printf("\n=================\n");

}
}

void getSizeOfArray(int amount[2],int price[2][4],int total[2])
{

}

最佳答案

如果你想获取金额数组大小:

int getSizeOfArray(int* amount)
{
    return (sizeof(amount) / sizeof(int));
}

它将返回 2

如果您想获取价格数组大小:

int * getSizeOfArray(int** price)
{
    int size[2];
    size[1] = (sizeof(price[0]) / sizeof(int));
    size[0] = (sizeof(price) / sizeof(int)) / size[1];
    return size;
}

它将返回一个数组:

大小[0]:2

大小[1]:4

关于c - 发送一个二维数组来计算总数并在函数中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58254444/

相关文章:

c - 将 C 程序设置为行缓冲区将不起作用

c - AIX下128位比较(64位)对hash值排序

c - C/C++ 中的编码字符串压缩算法

c - 在 while 循环中使用 wait() 而不是 waitpid()

c - 数组变量使用多少空间

c - 如何对窗口进行编程以使用退出键关闭

c - 有没有办法在C预处理器中执行任意代码?

从命令行使用 Visual C++ 2013 编译 C/SDL 程序

c# - 通过套接字发送图像并将其保存在服务器上

c - 引用 fgets,\0 如何合并到普通文本文件中