'often said that c++ is superset of c but below not following that [duplicate]
#include <stdio.h>
void print(int m, int n, int arr[][n])
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", arr[i][j]);
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print(m, n, arr);
return 0;
}
why this code is working in c but not in c++. seems like 2d array as a parameter with this does not work in c++.
Solution 1:[1]
Linked questions explain all the differences, but answering just for the code in the question,
why this code is working in c but not in c++
Code uses variable length array, VLA for short. C++ simply does not have that, so the code is invalid as C++.
Note that some compilers have extensions which may enable some non-standard features, such as VLA support in GNU C++. But it's generally a bad idea to use those, as there are several popular C++ compilers, and using non-standard extensions stops the code from compiling with other C++ compilers which don't have the same extension (or same syntax for the extension).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
