Y
Y
yunikeil2021-12-19 03:43:05
C++ / C#
yunikeil, 2021-12-19 03:43:05

An assignment action, to an element of a dynamic array, within a function, assigns that value to all copies of the array. What to do?

Help, please, how to get rid of such tricks, with inside functions, and how does it even work?int** array[i][j]

My code
#include <iostream>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;

void prog(int** a) {
    int** g = a;
    int** gg = a;

    for (int i = 0; i < 3; ++i){
        for (int j = 0; j < 3; ++j) {
            cout << a[i][j] << " ";
        }
         cout << endl;
    }

    g[1][1] = 1;
    cout << endl;

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << g[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << gg[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    setlocale(0, "");
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int** meza = new int* [3];
    for (int i = 0; i < 3; ++i)
        meza[i] = new int[3];

    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            meza[i][j] = 0;

    prog(meza);

    cout << "\n\nEnd of program, xx: " << endl;
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yunikeil, 2021-12-19
@yunikeil

Just replaced with pointer entry, loop, problem solved

#include <iostream>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;

void prog(int** a) {


    int** g = new int* [3];
    for (int i = 0; i < 3; ++i)
        g[i] = new int[3];

    int** gg = new int* [3];
    for (int i = 0; i < 3; ++i)
        gg[i] = new int[3];

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            g[i][j] = a[i][j];
        }
    }

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            gg[i][j] = a[i][j];
        }
    }

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
 
    g[1][1] = 1;
    cout << endl;

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << g[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << gg[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    setlocale(0, "");
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int** meza = new int* [3];
    for (int i = 0; i < 3; ++i)
        meza[i] = new int[3];

    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            meza[i][j] = 0;

    prog(meza);

    cout << "\n\nEnd of program, xx: " << endl;
    return 0;
}

G
galaxy, 2021-12-19
@galaxy

gand ggare not copies of an array, but copies of a pointer. Allocate memory for new arrays and copy withmemcpy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question