[SOLVED] Error: expected primary-expression before ']' token in C++


I’m trying to create something similar to string.find with vectors c++98, but I can’t fix this error.
I have simplified the code and left only the error.
If I can get help on how to achieve this.

#include <iostream>
#define MAX 99999
using namespace std;

int free_pos (int v[]);
int find_line (int v[], int pos);

int main() {
   char text[MAX];
   int loc_key[MAX],start_line[MAX]; //

   for(int i=0; i<free_pos(loc_key);i++) {
       cout << "Line: " << i+1;
       int pos = find_line(start_line[],loc_key[i]); //Here is the error
       for(int j=start_line[pos];j<start_line[pos+1];j++) {
   return 0;

int free_pos (int v[]) {
    for(int i=0;i<MAX;i++){
        if(v[i] == 0)
            return i;
    return 99;

int find_line (int v[], int pos) {
    for(int i=0; i<free_pos(v); i++) {
            return v[i];
        if(v[i]< pos)
            return v[i-1];


Your both functions

int free_pos (int v[]);
int find_line (int v[], int pos);

take arrays as an input, but then you try to call int pos = find_line(start_line[],loc_key[i]);, which should take the name of an array only, as it is the name which is known by the program as an array. You can easier (in my opinion) get it, if you write both of these functions following way:

int free_pos (int* v);
int find_line (int* v, int pos);

They do the same job as your functions, but you can see that their arguments are: an int pointer (array) and an int. Then, calling the function needs only the int pointer, which in your case is start_line, and the integer loc_key[i]. If you still have problems understanding it, you could read about dynamic memory allocation of arrays, which is done as: int* arr; arr = new int[your_size]; – I think it is clearer while thinking that way.

Answered By – whiskeyo

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *