1

I am trying to fix a code with errors, and I have one more error left I cannot figure out. The error says: expected primary-expression before ']' token, what does this mean? I have checked for misplaced semicolons and variable names, but cannot find anything. Here is my code(I have commented the line with an error):

// countOnes.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
void countOnes( int array[] ); // Count the number of 1s in a given int array.
const int arraySize = 10;
int array[ arraySize ];
int countOne = 0;
int main()
{
 for ( int i = 0; i <= arraySize; i++ )
 {
 array[ i ] = rand() % 3; // Fill array with 0, 1 or 2 randomly
 cout << array[ i ] << " ";
 }
 countOnes( array[], arraySize ); //ERROR
 cout << "\n The number of 1s in the array: " << countOne;
 return 0;
}
void countOnes( int array[], int arraySize )
{
 for ( int i = 0; i <= arraySize; i++ )
 if ( array[ i ] == 1 )
 countOne = countOne + 1;
 return;
}
Bill
14.8k4 gold badges45 silver badges55 bronze badges
asked Apr 19, 2013 at 17:40
2
  • You should consider using std::vector in c++ Commented Apr 19, 2013 at 17:44
  • Its not an runtime-error, its a compiler-error Commented Apr 19, 2013 at 17:45

4 Answers 4

4

No square brackets needed.

countOnes(array(削除) [] (削除ここまで), arraySize);

answered Apr 19, 2013 at 17:42
Sign up to request clarification or add additional context in comments.

Comments

2
countOnes( array[], arraySize ); //ERROR

You don't need [] here
Also declaration of

void countOnes( int array[] ); // Count the number of 1s in a given int array.

Doesn't correspond to its definition

void countOnes( int array[], int arraySize )
answered Apr 19, 2013 at 17:43

1 Comment

@user2085224 I can ask the same way why would you do? When you declare function, you declare it in a way that it will accept array as 1st param. And when you call it compiler already knows that array is indeed of type array(not so good name for an variable btw). Thus no additional hints required. Or did you mean something else when putting []?
1

'array' is treated as reserved word in Microsoft's extension of C. See Why is "array" marked as a reserved word in Visual-C++?. Replace 'array' by something else, e.g. 'arr'.

answered Apr 19, 2013 at 17:52

1 Comment

That thread, despite its name, does not establish that Microsoft's compiler treats array as a reserved word in C or C++. It's about the syntax highlighter. Treating array as a reserved word would violate the language definition.
0

If you want to pass an array, you can pass a pointer to the first element to the array:

 void countOnes( int* array, int size );
answered Apr 19, 2013 at 17:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.