Home > Blockchain >  i want to call this 3d array
i want to call this 3d array

Time:10-04

how can i this function this is an 3D array i want to scan from user the number of row ,colum,cell then send 3D array


   #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
    
    }

in this code i have problem

CodePudding user response:

void School (size_t layers, size_t col, size_t cell, int (*ptr)[col][cell])
{
        
}

CodePudding user response:

First you need to update the function prototype to accept dimensions before the array parameter (pointer to array actually).

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

Then invoke it as:

School(row, col, cell, arr); 

The actual scanning code would be:

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 has extensions that allows to declare the parameters, before passing it.

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

This would let you call the function as:

School(arr, row, col, cell);
  • Related