error: invalid operands of types 'char* (*)(const char*, int)' and 'const int'

Hi all,

I am pretty new to Arduino and I am writing a sketch for an electronic Vertical Speed Indicator for a light aircraft.

Yesterday my sketch was working fine but since updating the IDE this morning, when I verify the sketch, it gives me the following message:

error invalid operands of types ‘char* ()(const char, int)’ and ‘const int’ to binary ‘operator%’
index = (index + 1) % window_size; // Increment the index, and wrap to 0 if it exceeds the window size

I have stared at this problem for a few hours and can’t find the problem as it seems to me that the ‘index’ remains an int throughout the sketch.

Any help would be appreciated!

Here is my code:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP3XX.h>
#include <bmp3.h>
#include <bmp3_defs.h>
#include “Adafruit_BMP3XX.h”

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp(BMP_CS); // hardware SPI

const float refreshRate = 12.5;
//RefreshRate is the rate of data capture per second (Hz).
const int window_size = 5;
//windowSize determines how many consecutive readings are used for moving average to reduce noise.
float altitude_last[1];
float averaged_altitude_last[1];
unsigned long StartTime[1];
unsigned long LoopStartTime[1];
float altitude_difference;
float averaged_altitude_difference;
int index; //This is highlighted as the source of the error.
float sum;
float readings[window_size];
float averaged_altitude;

void setup() {
Serial.begin(115200);
bmp.begin();
bmp.setPressureOversampling(BMP3_OVERSAMPLING_16X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_7);
bmp.setOutputDataRate(BMP3_ODR_25_HZ);
}

void loop() {

if (millis() - LoopStartTime[0] >= (1000/refreshRate))
{
LoopStartTime[0] = millis();
float altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA); // altitude in metres
unsigned long EndTime = millis();
unsigned long ElapsedTime = EndTime - StartTime[0];
StartTime[0] = millis();
float altitude_feet = altitude * 3.28084;
sum = sum - readings[index]; // Remove the oldest entry from the sum
readings[index] = altitude_feet; // Add the newest reading to the window
sum = sum + altitude_feet; // Add the newest reading to the sum
index = (index + 1) % window_size; // Increment the index, and wrap to 0 if it exceeds the window size
averaged_altitude = sum / window_size;
altitude_difference = (altitude_feet - altitude_last[0]);
averaged_altitude_difference = (averaged_altitude - averaged_altitude_last[0]);
altitude_last[0] = altitude_feet;
averaged_altitude_last[0] = averaged_altitude;
float vsi = altitude_difference * 60.0 * (1000.0 / ElapsedTime);
float averaged_vsi = averaged_altitude_difference * 60.0 * (1000.0 / ElapsedTime);
Serial.println((String)"Altitude: " + altitude_feet + (String)“ft”);
Serial.println((String)"Averaged Altitude: " + averaged_altitude + (String)“ft”);
//Serial.println((String)"1: " + readings[0]);
//Serial.println((String)"2: " + readings[1]);
//Serial.println((String)"3: " + readings[2]);
//Serial.println((String)"4: " + readings[3]);
//Serial.println((String)"5: " + readings[4]);
Serial.println((String)"Elasped Time: " + ElapsedTime + (String)“ms”);
Serial.println((String)"Alt Diff: " + (altitude_difference) + (String)“ft”);
Serial.println((String)"Ave Alt Diff: " + (averaged_altitude_difference) + (String)“ft”);
Serial.println((String)"Vertical Speed: " + vsi + (String)“ft/min”);
Serial.println((String)“Ave Vertical Speed: " + averaged_vsi + (String)“ft/min”);
Serial.println(” ");
}
}

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here

index() is a string function on some systems (e.g. ARM, not on AVR).
This means you can not have a global variable with the same name.

$ man index

NAME
index, rindex - locate character in string

SYNOPSIS
#include <strings.h>

char *index(const char *s, int c);

char *rindex(const char *s, int c);

DESCRIPTION
The index() function returns a pointer to the first occurrence of the character c in the string s.

The rindex() function returns a pointer to the last occurrence of the character c in the string s.

The terminating null byte (’\0’) is considered to be a part of the strings.

RETURN VALUE
The index() and rindex() functions return a pointer to the matched character or NULL if the character is not found.
. . .
CONFORMING TO
4.3BSD; marked as LEGACY in POSIX.1-2001. POSIX.1-2008 removes the specifications of index() and rindex(),
recommending strchr(3) and strrchr(3) instead.

float altitude_last[1];
float averaged_altitude_last[1];
unsigned long StartTime[1];
unsigned long LoopStartTime[1];

One element arrays?

You might be able to make index a local static to loop(). Then is can have only local scope but
global extent (= lifetime).

error invalid operands of types 'char* ()(const char, int)' and 'const int' to binary 'operator%'
index = (index + 1) % window_size; // Increment the index, and wrap to 0 if it exceeds the window size

This error states that the left-hand value to the '%' operator is a pointer to a function, and that that makes no sense. Specifically, it's a pointer to a function taking a char* and and int as its arguments, and returning a pointer to char.

As oqibidipo explains - 'index' is the name of a library function. Pick another name for your variable.

oqibidipo:
index() is a string function on some systems (e.g. ARM, not on AVR).
This means you can not have a global variable with the same name.

$ man index

NAME
index, rindex - locate character in string

SYNOPSIS
#include <strings.h>

char *index(const char *s, int c);

char *rindex(const char *s, int c);

DESCRIPTION
The index() function returns a pointer to the first occurrence of the character c in the string s.

The rindex() function returns a pointer to the last occurrence of the character c in the string s.

The terminating null byte (’\0’) is considered to be a part of the strings.

RETURN VALUE
The index() and rindex() functions return a pointer to the matched character or NULL if the character is not found.
. . .
CONFORMING TO
4.3BSD; marked as LEGACY in POSIX.1-2001. POSIX.1-2008 removes the specifications of index() and rindex(),
recommending strchr(3) and strrchr(3) instead.

Thanks, this was the issue! I got this error when I tried my code on a teensy which is a teensy. I have changed str index() to str increment() and it works again. Thank you.

groundFungus:
float altitude_last[1];
float averaged_altitude_last[1];
unsigned long StartTime[1];
unsigned long LoopStartTime[1];

One element arrays?

Obvious I am new to arduino??

My sketch takes data from the last loop and compares it to the updated the pressure reading and so for the first iteration for the loop, I need several floats to be already defined and loaded with data. When I set a value to each float using float altitude_last = 100, my code did not work as it then would not update the global float value on subsequent iterations. However, I found by making the global float a one element array, it would allow the first loop calculation to be completed and subsequent loops to then update the array.

I am very new to this and will likely find a better way of achieving this and will optimise my code accordingly.

Thanks for the help,

Jacks

4535jacks:
My sketch takes data from the last loop and compares it to the updated the pressure reading and so for the first iteration for the loop, I need several floats to be already defined and loaded with data. When I set a value to each float using float altitude_last = 100, my code did not work as it then would not update the global float value on subsequent iterations. However, I found by making the global float a one element array, it would allow the first loop calculation to be completed and subsequent loops to then update the array.

But you never initialize altitude_last[0]? There is no reason I can see in your code that you need one element arrays. Actually, I don't know any reason to have one element arrays.