Home > OS >  How would I fix this to not be an array of reference?
How would I fix this to not be an array of reference?

Time:10-16

Preface: Yes I am aware of the inconsistency in function definitions, I am in the process of trying to write the thing.

Specifically in line

void renderScreen(char& currentMap[100][100], int& screenX, int& screenY)

The char& currentMap[100][100] creates an array of references. How would I call a separate char variable based on a 2D map (array of chars) into this function without making an array of references?

Context:

#include <iostream>
#define _WIN32_WINNT 0x0500 //win2000, must be before #windows.h
#include <windows.h>
#include <strsafe.h>
#include "declarations.h"

using namespace std;

int main(int screenX, int screenY)
{
    SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);

    while (!quit) {
        
    }
    return 0;
}

void renderScreen(char& currentMap[100][100], int& screenX, int& screenY) {
    int xi; int yi;

    for (xi = 0; xi < screenX; xi  ) {
        for (yi = 0; yi < screenY; yi  ) {
            screen  = currentMap[xi][yi];
        }
        cout << screen << endl;
        screen.clear();
    }

}

.h file

#pragma once
//fns
void renderScreen(char& currentTile, int& screenX, int& screenY);

//vars
string screen;
bool quit = false;
int i; int j;
int screenX; int screenY;
char currentMap[100][100];
char currentTile;

CodePudding user response:

I do not see any sense in the second and third parameter declarations declared as references

void renderScreen(char& currentMap[100][100], int& screenX, int& screenY);

because within the function the original objects used as arguments are not changed within the function. So the function could be declared at least like (if to ignore the incorrect declaration of the first parameter)

void renderScreen(char& currentMap[100][100], size_t screenX, size_t screenY);

Also there is no great sense to pass the array by reference.

The function could be declared like

void renderScreen(const char ( *currentMap )[100], size_t screenX, size_t screenY);

If to pass the array by reference then the function declaration can look like

void renderScreen(const char ( &currentMap )[100][100], size_t screenX, size_t screenY);
  • Related