C
C
CodeInside2015-10-31 01:22:25
C++ / C#
CodeInside, 2015-10-31 01:22:25

What is the correct way to pass a reference to a two-dimensional array to a function?

There is a file with lines (answer options). In this file, rowsets are separated by an empty line (4 options -> empty line -> 4 options, etc.). In the scope of the main function, there is a two-dimensional array of strings response[15][4]. I have a syntax error when passing the address of an array to a function. Who can correct / suggest?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void readResponses(string,string**);

void main()
{

  string response[15][4];//варианты ответов (15 вопросов * 4 варианта)

  readResponses("responses.txt",response);//здесь синтаксическая ошибка, ибо функция не может принять указатель на двумерный массив
}

void readResponses(string address,string** rPointer)//считывает варианты ответов с файла по полученному адресу и записывает их в строки указателя
{
  ifstream fResponses(address);
  string empty;//для разделения сетов строк
  for(int i =0; i< 15; i++)//15 вопросов
  {
    for(int j = 0; j < 4; j++)//4варианта ответов
    getline(fResponses,rPointer[i][j]);//считывание и запись строки
  }
  getline(fResponses,empty);//сэты вариантов ответа разделены пустой строкой -> переход на следующий сэт
}
}

When I wrote the question, I thought: or is the name of a two-dimensional array not a pointer to a pointer? :|

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2015-10-31
@CodeInside

void readResponses(string address, string (&rResponse)[15][4]) {}

A two-dimensional array is NOT pointers to a pointer, this is not Java for you. This is an array of one-dimensional arrays.
There is no normal way to transfer arrays of fixed length in C (only through a pointer to the first element), in C++ - through a reference.

O
Oleg Tsilyurik, 2015-10-31
@Olej

or is the name of a two-dimensional array not a pointer to a pointer?

Is an.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question