I am making a C++ program and I have a warning that keeps cropping up (I'm using g++):
warning: pointer to a function used in arithmetic [Wpointer-arith]
and I want to know: what exactly does this warning message mean? What is my compiler trying to tell me the problem is (in general terms) so I can better understand what I'm doing wrong?
Google searches turn up specific solutions to specific problems in people's code, but never tell me exactly what this warning message is trying to say.
I'm just learning arrays and I'm trying to make a program that prints "Hello, world!" one character at a time, each character being stored individually in an array and pumped to cout in a for loop.
Here is the code:
#include <iostream>
using namespace std;
int ARRAY_ELEMENTS = 14;
void greeting()
{
char greeting[ARRAY_ELEMENTS];
greeting[0] = 'H';
greeting[1] = 'e';
greeting[2] = 'l';
greeting[3] = 'l';
greeting[4] = 'o';
greeting[5] = ',';
greeting[6] = ' ';
greeting[7] = 'w';
greeting[8] = 'o';
greeting[9] = 'r';
greeting[10] = 'l';
greeting[11] = 'd';
greeting[12] = '!';
greeting[13] = '\0';
}
int main(int argc, char* argv[])
{
greeting();
for (ARRAY_ELEMENTS = 0;
ARRAY_ELEMENTS <= 13;
ARRAY_ELEMENTS++)
{
cout << greeting[ARRAY_ELEMENTS] << endl;
}
return 0;
}
Thank you for your time.
On this line:
cout << greeting[ARRAY_ELEMENTS] << endl;
you are referring to the function named greeting that you're treating as if it were an array.
greeting is a function, but you try to print it out like it's a char array. It doesn't help the clarity of your code that the function greeting() contains a char array by the same name - this is probably where you've gotten confused. If you give things distinct names, it would be more obvious what's going wrong.
You need to have your greeting() function return something, rather than just fill in a local array, which will not be accessible from outside of the function (and in any event will be discarded as soon as the function returns).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With