如何实现此功能这是一个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
}
在这个代码中,我有问题
首先,您需要更新函数原型以接受数组参数之前的维度(实际上是指向数组的指针)。
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);
void School (size_t layers, size_t col, size_t cell, int (*ptr)[col][cell])
{
}