【问题标题】:i want to call this 3d array我想调用这个 3d 数组
【发布时间】:2021-11-24 10:35:51
【问题描述】:

我怎样才能使用这个功能 这是一个 3D 数组 我想从用户扫描行数、列数、单元格数,然后发送 3D 数组


   #include <stdio.h>

    void School (int(*ptr)[int col][int cell] );//this is a user function 

    void main (void){
    int row ;//number of row 
    int col ;//number of colum
    int cell ;//number of cell
    
    printf("Enter Number OF School Layers \n");
    scanf("%d",&row);
    printf("Enter Number OF Classes in each Layer \n");
    scanf("%d",&col);
    printf("Enter Number OF Students in each Class \n");
    scanf("%d",&cell);
    

    
    int arr [row][col][cell];
    
    

    School(arr,row,col,cell);//calling of function 

    }
    void School (int(*ptr)[int col][int cell] ){//i want what write here
    
    }

在这段代码中我有问题

【问题讨论】:

  • 我想从用户那里扫描行数、列数和单元格数,然后发送 3D 数组
  • “发送 3D 数组”是什么意思?那会是什么结果呢?
  • "我想从用户那里扫描行数、列数、单元格数"这部分似乎已经由你的代码完成了。
  • School(arr,row,col,cell);//calling of function 与原型void School (int(*ptr)[int col][int cell] );//this is a user function 不匹配。尝试调用提供了四个参数,原型声明应该只有一个。你能决定你想要哪一个吗?

标签: arrays c function pointers


【解决方案1】:

首先你需要更新函数原型以接受数组参数之前的维度(实际上是指向数组的指针)。

void School (int rows, int cols, int cells, int arr[rows][cols][cells]);

然后调用它:

School(row, col, cell, arr); 

实际的扫描码是:

void School (int rows, int cols, int cells, int arr[rows][cols][cells]) {
for (int row = 0; row < rows; ++row)
for (int col = 0; col < cols; ++col)
for (int cell = 0; cell < cells; ++cell)
   scanf("%d", &arr[row][col][cell]);
}

GCC 具有允许在传递参数之前声明参数的扩展。

void School (int rows, cols, cells; int arr[rows][cols][cells], int rows, int cols, int cells);

这会让你调用函数:

School(arr, row, col, cell);

【讨论】:

  • 非常感谢我的朋友,我很感谢你帮助我..
【解决方案2】:
void School (size_t layers, size_t col, size_t cell, int (*ptr)[col][cell])
{
        
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2022-01-13
    • 2016-05-31
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多