How can I create an 2d array in one function and then pass it to some other function which prints it?
            Asked
            
        
        
            Active
            
        
            Viewed 38 times
        
    -3
            
            
        - 
                    2Possible duplicate of [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – Killzone Kid Jun 20 '18 at 17:16
1 Answers
-2
            
            
        Because it took me ages to find this I'm just gonna put this here. My goal was to create a 2d array, pass it to some function (in this case a 'printer') and then let that function print the 2d array for me because I didn't want to repeat it and/or write it every sexing time to know what the 2d array looked like.
void pre(int *grid, int x, int y) {
    const int base = 8;
    for (int y = 0; y < base; y++) {
        for (int x = 0; x < base; x++) {
            printf("%d ", *(grid+x*base+y));
        }
        printf("\n");
    }
}
int main(int argc, char **argv) {
    const int base = 8;
    int cursor = 0;
    int grid[base][base];
    for (int y = 0; y < base; y++) {
        for (int x = 0; x < base; x++) {
            grid[x][y] = x;
        }
    }
    pre((int*)grid, base, base);
}
PS1: Source: YouTube video by CppNuts
PS2: I sincerely hope someone is helped with this, not a weird attempt for upvotes.
 
    
    
        Bas van Ommen
        
- 1,243
- 2
- 12
- 20
