Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
using namespace std;

int main()
{
// an array with 1 row and 0 column.
int arr[1][0] ={1};
int i, j;

cout<<"Printing a 2D Array:\n" ;

// output each array element's value
for(i=0;i<1;i++)
{
for(j=0;j<0;j++)
{
cout<<"\t"<<arr [i][j];
="" }
="" cout<<endl;
=""
="" return="" 0;
}

<b="">What I have tried:

main.cpp: In function ‘int main()’:
main.cpp:15:22: error: too many initializers for ‘int [1][0]’
15 | int arr[1][0] ={1};
| ^

It becomes like this, so that I don't know to solve it.
Posted
Updated 23-Nov-22 23:47pm

A bidimensional array has (rows x columns) items.
Since, in your case, columns = 0, the array has 0 items. The compiler, as usual, is correct.
 
Share this answer
 
"1 row and 0 column", I think it doesn't have space to store any element, but
int arr[1][0] ={1};

tried to store an element to array, that's not correct,
if you need an array that every element is also an array, and it includes one element like this, you can code:
#include <vector>
#include <iostream>
using namespace std;
void test()
{
	vector< vector<int> > v(1);
	cout << "v.size ==" << v.size() << "; v[0].size == " << v[0].size() << endl;
}


or you can code:
int* arr[1]; //arr[1] is a pointer, and it can pointer to an int-array later
int arr1[] = { 1,2,3 };
arr[0] = arr1; // now arr[0][1] == 2
 
Share this answer
 
C++
// an array with 1 row and 0 column.
int arr[1][0] ={1};

That does not make sense, an array cannot have a dimension of zero length. And why would you want an array that contains only a single cell? You need something like:
C++
// an array with 1 row and 0 column.
int arr[2][2] ={ {1, 2}, { 3, 4} };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900