I am attempting to pass a 2D array of integers from my main function in a cpp program to another function, and to manipulate the 2D array in this other function. While I've done this before, it's been a while, so I was following this accepted answer:
Direct link to answer in question the below program is modeled directly after
However, while everything looks ok to me, 2/3 of the methods suggested in the answer are failing. I've stripped out anything unrelated to the error in what I've pasted below to hopefully make it easy to see what I mean.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int LINES_IN_FILE = 500;
int NUMS_PER_LINE = 4;
void change2dArrayMethod1(int (*lines)[LINES_IN_FILE][NUMS_PER_LINE]) {
(* lines)[0][0] = 1;
(* lines)[0][1] = 2;
(* lines)[0][2] = 3;
(* lines)[0][3] = 4;
}
void change2dArrayMethod2(int lines[][NUMS_PER_LINE]) {
lines[0][0] = 1;
lines[0][1] = 2;
lines[0][2] = 3;
lines[0][3] = 4;
}
void change2dArrayMethod3(int lines[]) {
lines[0] = 1; //not sure how to access entire array here
}
int main() {
int coordLines[LINES_IN_FILE][NUMS_PER_LINE];
// METHOD 1
// Fails with error:
// Cannot initialize a variable of type 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]'
// with an rvalue of type 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]'clang(init_conversion_failed)
int (*p1_coordLines)[LINES_IN_FILE][NUMS_PER_LINE] = &coordLines;
// Fails with error:
// No matching function for call to 'change2dArrayMethod1'clang(ovl_no_viable_function_in_call)
// test.cpp(10, 6): Candidate function not viable: no known conversion from 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]' to
// 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]' for 1st argument
change2dArrayMethod1(p1_coordLines);
// METHOD 2
// Fails with error:
// Cannot initialize a variable of type 'int (*)[NUMS_PER_LINE]' with an lvalue of type 'int [LINES_IN_FILE][NUMS_PER_LINE]'clang(init_conversion_failed)
int (*p2_coordLines)[NUMS_PER_LINE] = coordLines;
// Fails with error:
// No matching function for call to 'change2dArrayMethod2'clang(ovl_no_viable_function_in_call)
// test.cpp(17, 6): Candidate function not viable: no known conversion from 'int (*)[NUMS_PER_LINE]' to 'int (*)[NUMS_PER_LINE]' for 1st argument
change2dArrayMethod2(p2_coordLines);
// METHOD 3
// Doesn't fail - however not sure how to manipulate array in function called
int *p3_coordLines = coordLines[0];
change2dArrayMethod3(p3_coordLines);
}
Additionally, when using the 3rd method suggested, I'm not sure how assignment works, or even how to access values in the array.
I've pasted the errors the clang compiler gives in comments above each call to the second function. There are no errors in the functions other than main, which are the ones taken directly from the answer from the above link. However, I've also passed the 2D array in the same way the above link suggested for each method, so I'm really at a loss as to what is wrong here.
CodePudding user response:
An array when used in an expression decays to a pointer to its first element.
So a variable of type int [LINES_IN_FILE][NUMS_PER_LINE]
decays to type int (*)[NUMS_PER_LINE]
, and the latter in a function declaration can also be expressed as int [][NUMS_PER_LINE]
So you want to use this function:
void change2dArrayMethod2(int lines[][NUMS_PER_LINE]) {
And pass the array directly:
change2dArrayMethod2(coordLines);
Also, C doesn't allow non-const values for array indices. You'll need to make the values const
:
const int LINES_IN_FILE = 500;
const int NUMS_PER_LINE = 4;