pointers problem. Any help?

It gives me an error of invalid types "int[int] for array subscript in line 10
How do I fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>
#include <fstream>
using namespace std;
void riga(ofstream &put,int &val2, int n_el, int X[], int pr, int sec, int ter,int j)
{
			for(int k=0;k<sec &&val2<n_el;k++)
				{	put<<"r"<<k<<":"<<"";
					for(int l=0; l<ter &&val2<n_el;l++)
					{
						X[j][k][l]=X[sec*j + ter*k + l];
						put<<X[j][k][l]<<" ";
						val2++;
					}
					put<<"\n";
				}		
	}
	void funzione(ofstream &out, int n_el, int X[], int pr, int sec, int ter )
	{	int Z[pr][sec][ter];
		int val=0;
		for(int j=0;j<pr &&val<n_el; j++)
		{	
			out<<"\n"<<"strato"<<" "<<j<<endl;
			riga(out,val, n_el, X, pr, sec, ter,j);
		}
	}
		
int main()
{
	ifstream IN("input");
	ofstream OUT("output");
	if (IN && OUT)
	{
		int n_el; //Leggo array A[400]
		int X[400];
		IN>>n_el;
		for(int i=0; i<n_el; i++)
			{
				IN>>X[i];
			}	// finisco di leggere l'array dichiarato
			int a, b, c;
			IN>>a>>b>>c;
			funzione(OUT, n_el, X, a,b,c);
	}
	else
	cout<<"errore con i files";
	IN.close(); OUT.close();    
	}
X[j][k][l] What are you trying to do here? X is just a normal Array. And here you use it as a 3 dimensional array?

Edit : in your function, you create int Z[pr][sec][ter]; but then you never use it. You use X which is just a normal array instead of Z.
Last edited on
oh man! Thanks know I saw the problem and I've been able to fix it =)
As i understood, X is a 1D array, but in line 10, you want to get access to X as 3D array!
so, it should be impossible!
its the reason of this error:
In function 'void riga(std::ofstream&, int&, int, int*, int, int, int, int)': 10:13: error: invalid types 'int[int]' for array subscript
Last edited on
Glad I could help you :)

Happy Coding!
Topic archived. No new replies allowed.