Difficulty writing contents to array

I'm trying to write the contents of a file to an array, then in another function display the contents of the array. However i'm getting held up at line 43 with this compiler error.

|43|error: invalid types 'int[int]' for array subscript|

I don't understand what's wrong with the type. I definitely want the size declorator of the array to be of the data type int. I'm looking at an example in my book and it has a similar use of syntax. Also when I build my code (code::blocks, compiler C++11) I see a stop sign on line 16. What is that? For some reason that stop sign doesn't show up as an error in my build messages. Is it just there because there's a problem related to that function?


Here's my code.
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
/*In general this project will be a go to project that can be used
to practice any programming skills I'd like to sharpen.*/


/*This program will create a file, then write contents to it.
The program will then store the contents of that file to an array.
*/
#include <iostream>
#include <fstream>
using namespace std;

/*This function will make a file.*/
void makeFile();

/*This function will dump contents to the file made.*/
void fileDump(int [], int);

const int SIZE = 10;
int dispNumbs[SIZE];
fstream skeletonFile;

int main()
{
	makeFile();
	fileDump(dispNumbs, SIZE);
	cout << "Good Day sir!";
    return 0;
}

void makeFile()
{
	//This section will begin making the file

	skeletonFile.open("Months.txt", ios::app);
	skeletonFile.close();
}

void fileDump(int displayarray, int SIZE)
{
	//write contents of file to array
	for (int count = 0; count < SIZE; count++)
	{
		skeletonFile << displayarray[count] << endl;
	}
}
  
Last edited on
Are lines 16 and 38 identical on argument types?
Yes, the arguments in line 16 void fileDump(int [], int); and line 38 void fileDump(int displayarray, int SIZE) are identical argument types int .
I'll remove those distracting names:
1
2
void fileDump(int [], int)
void fileDump(int   , int)

They do not look same to me.
The first takes a pointer to int and an int.
The second takes an int and an int.
These are two different functions.

Pointers use the asterisk '*' symbol as the indirection operator. I used the square brackets '[]' to denote an array. I'm looking through my text book and I'm not seeing anything that says the square brackets in the function parameters are pointers. Also when I deleted the brackets I got these errors:

1
2
3
4
|25|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
|16|note: initializing argument 1 of 'void fileDump(int, int)'|
|In function 'void fileDump(int, int)':|
|43|error: invalid types 'int[int]' for array subscript|


Last edited on
The dispNumbs is not an int. It is an array of integers. However, on line 25, when you do call a function with dispNumbs as parameter the compiler states that it is a pointer to int (int*). How can that be?

An array is not a pointer either. They are different types. However, there is an implicit conversion from array to pointer; array decays to pointer.

The size of an array is known at compile-time. A pointer does not know whether it points to zero, one or array member of valid objects. Hence "decay" as in "lose something".


Function arguments are by value (by default); they are copies of values that the function is called with. An array can be large. To copy array can thus be relatively expensive. The designer's of C chose that there are no "by value array function arguments". They chose that a pointer is used instead. It is a by value argument, but what is copied is the address of the first element of the array. Due to the decay you have to pass the array size as separate function argument.

While the argument type in reality is a pointer void fileDump(int *, int)
one can use the bracket syntax void fileDump(int [], int) too to hint that
the function operates on array.


Both arrays and pointers are dereferenced. Again, the syntax is interchangeable:
1
2
3
4
5
6
7
8
9
10
*(foo + n)
// is same as
foo[n]

// and
foo[0]
// is same as
*(foo + 0)
// is same as
*foo

I understand that pointers and arrays are basically the same thing. I know that when the command cout << arrayname; is executed, the beginning memory address of the array is actually sent out, and not all of the array elements. I know this. In this program I don't want to use pointers, so I'd like to stop talking about them. I want to know how to correct the error in lines, 25, and 43 of this code. Here are their error messages:

1
2
3
|25|error: expected primary-expression before ']' token|
|In function 'void fileDump(int, int)':|
|43|error: invalid types 'int[int]' for array subscript|


Here's the rest of the code:

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
/*In general this project will be a go to project that can be used
to practice any programming skills I'd like to sharpen.*/


/*This program will create a file, then write contents to it.
The program will then store the contents of that file to an array.
*/
#include <iostream>
#include <fstream>
using namespace std;

/*This function will make a file.*/
void makeFile();

/*This function will dump contents to the file made.*/
void fileDump(int , int);

const int SIZE = 10;
int dispNumbs[SIZE];
fstream skeletonFile;

int main()
{
	makeFile();
	fileDump(dispNumbs[], SIZE);
	cout << "Good Day sir!";
    return 0;
}

void makeFile()
{
	//This section will begin making the file

	skeletonFile.open("Months.txt", ios::app);
	skeletonFile.close();
}

void fileDump(int displayarray, int SIZE)
{
	//write contents of file to array
	for (int count = 0; count < SIZE; count++)
	{
		skeletonFile << displayarray[count] << endl;
	}
}
In this program I don't want to use pointers

The language, however, does.

Line 16: void fileDump( int *, int );
Line 38: void fileDump( int * displayarray, int SIZE )

If you want to pretend that there are no pointers, then apply syntactic sugar:
Line 16: void fileDump( int [], int );
Line 38: void fileDump( int displayarray[], int SIZE )


PS. You do close the file on line 35. By the time you call fileDump(), the stream is closed and writing to a closed stream is a runtime error.

Another recommendation: do not use global variables. Pass data to functions via parameters.

However, do note that one should not copy stream objects. You have to use by reference arguments. E.g.
void fileDump( int [], int, std::fstream & );
I made the changes but I'm still getting an error on line 25 about expecting a primary expression before the ']' token.

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
/*In general this project will be a go to project that can be used
to practice any programming skills I'd like to sharpen.*/


/*This program will create a file, then write contents to it.
The program will then store the contents of that file to an array.
*/
#include <iostream>
#include <fstream>
using namespace std;

/*This function will make a file.*/
void makeFile();

/*This function will dump contents to the file made.*/
void fileDump( int [], int, std::fstream & );

const int SIZE = 10;
int dispNumbs[SIZE];
fstream skeletonFile;

int main()
{
	makeFile();
	fileDump(dispNumbs[], SIZE);
	cout << "Good Day sir!";
    return 0;
}

void makeFile()
{
	//This section will begin making the file

	skeletonFile.open("Months.txt", ios::app);
	skeletonFile.close();
}

void fileDump( int displayarray[], int SIZE )
{
	//write contents of file to array
	for (int count = 0; count < SIZE; count++)
	{
		skeletonFile << displayarray[count] << endl;
	}
}
Yes, the line 25. Revert to: fileDump( dispNumbs, SIZE );
Why does C++ want to use pointers instead of passing an array in line 25? I mean I know it's possible to pass an array to a function. Is there some kind of syntax that i'm unknowingly using that makes C++ ask me to use a pointer? Is it 100% impossible for me to only use arrays in this program?
Why does C++ want to use pointers instead of passing an array in line 25?


It doesn't.

C++ wants you to use a vector. Then you can pass the vector in line 25. You're choosing to ignore what C++ wants you to do (use a vector) and instead you're insisting on coding in C.
Topic archived. No new replies allowed.