Инициализация массива. Нарушение прав доступа при чтении по адресу
Прошу подсказать как правильно инициализировать массив в моем случае?
//usinglib.cpp
#include <iostream>
#include "MassivFromMaxValue.h"
using namespace std;
const int n = 6, m = 4;
int main()
{
setlocale(LC_ALL, "Russian");
int * mas[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << "Введите элемент массива [" << i << "][" << j << "]: ";
cin >> *mas[i][j]; //Падает здесь. Из-за не инициализированного массива как я понимаю
}
}
cout << "Исходный массив:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << " " << *mas[i][j];
}
cout << endl;
}
cout << "Итоговый массив:" << endl;
int* target;
target = fnMassivFromMaxValue(*mas,n,m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << " " << *mas[i][j];
}
cout << endl;
}
}
На всякий случай функция:
#include "pch.h"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include "MassivFromMaxValue.h"
using namespace std;
int* fnMassivFromMaxValue(int * m[], unsigned rows, unsigned cols)
{
int* b = new int[cols];
for (size_t i = 0; i < rows; ++i) //Заполняем массив b
b[i] = *max_element(m[i], m[i] + cols);
return b;
}
Вот так не подходит
int * mas = new int*[n][m];
Очень прошу подсказать. Спасибо!
Ответы (1 шт):
Автор решения: Александр
→ Ссылка
Разобрался сам. Нужно было следующее:
int** mas = new int* [m]; // Создаем и инициализируем массив
for (int count = 0; count < n; count++)
mas[count] = new int[n];
И, естественно удалить все указатели (*) в main