0

error: expected initializer before 'int'

i am trying to pass 2D array as parameter in function, on building I got error written in title and error message pointed this line and pointed before int corresponding to size ...... => bool dia_sum(int arr[][int size]) { vector<int> sum{}; int add{}; ................... ............... return false } I am not able to resolve it actually can't understand what is it?

5th Jun 2021, 7:19 AM
saurabh
saurabh - avatar
6 Respostas
+ 3
Noob In C++ multidimensional arrays have to have fixed sizes for all dimensions but the first. You declared your function with int arr[][int size] which is not possible because these dimensions have to be known at compile time. So for example int arr[][5] would work. When you want to work with dynamic sized multidimensional arrays in C++ there are several ways to do this. For example you can just allocate a dynamic array like this: int* arr = new int[size*size]; and access elements like this: arr[x+y*size] this way you can pass the pointer to a function. Of course don't forget to delete[] arr; after you don't need it anymore. There are also other ways of doing this like using std::vector
5th Jun 2021, 7:39 AM
Hape
Hape - avatar
+ 1
The problem is literally what the compiler is saying "left operand of the comma operator has no effect" That is, the left side of the expression on line 104 "i >= 0, j < size2" ^^^^^^ has no effect, or in other words, is useless. This is because the comma operator ignores its left operand. See this: https://en.m.wikipedia.org/wiki/Comma_operator To fix it, use the logical AND operator (&&) instead of the comma "i >= 0 && j < size2" You need to do the same thing for line 109. Also, on line 37, you are making the same mistake you were making in your previous code `int matrix[size][size]` A non-const integer variable cannot be used to declare the size of the array as the size must be known to the compiler at compile time. You need to either use a integer literal (like "int matrix[2][2]") or a const variable. It works on GCC, but it doesn't work on most other compilers. Again you need to allocate a block of memory `int *matrix = new int[size][size];`
5th Jun 2021, 9:21 AM
XXX
XXX - avatar
+ 1
Noob sorry, I didn't read your "2nd query". The error is because it is invalid syntax. Just like the line `int i{0}, int j{1}` is invalid syntax, the line `for (int i{0}, int j{0};....)` is also invalid syntax. You only need to specify the type at the start of the declaration, like so `for(int i{0}, j{0};...)`
5th Jun 2021, 9:26 AM
XXX
XXX - avatar
0
can i see ur array and how you pass it?
5th Jun 2021, 7:21 AM
durian
durian - avatar
5th Jun 2021, 7:27 AM
saurabh
saurabh - avatar
0
@Hape thanks bro, I have modified my code ,working fine but still giving warning message warning: left operand of comma operator has no effect [-Wunused-value] in line........... for(i = size1-1,j=0; i>=0, j< size2 ; i--,j++) ............... but I couldn't figure out how comma operator is unused here? Also 2nd query is(not related to above , just a doubt).... if I do for(int i{0} ; i < 5 ; i++) //fine if I do for(int i{0} , int j{0}; i < 5 , j < 5; i++, j++) //gives error error says ,I) expected expression before int, II) can't do list initialization please tell me why ? and what is the reasons?
5th Jun 2021, 8:42 AM
saurabh
saurabh - avatar