What does this mean (C++): “warning: pointer to a function used in arithmetic [Wpointer-arith]”?

喜你入骨 提交于 2021-02-05 08:10:36

问题


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.


回答1:


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.




回答2:


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).




回答3:


The statement greeting[ARRAY_ELEMENTS] doesn't do what you think it does.

From inside main the name greeting refers to the function void greeting(). You seem to be trying to print the char greeting[ARRAY_ELEMENTS] variable. But you cannot do that from main since that name is only visible from inside void greeting().

You should rename either the function or the variable to ensure that this confusion does not happen again, and beyond that you you have two options: Either ove the loop inside the function greeting or pass a pointer to a buffer into which the greeting must be set into the function. This might be a little more advanced than your current level but you will have to learn how to do it eventually, so no time like the present.

const int ARRAY_ELEMENTS = 14;

void make_greeting(char *greeting)
{
    greeting[0] = 'H';
    greeting[1] = 'e';
    ...
}

int main(int argc, char **argv)
{
    char buffer[ARRAY_ELEMENTS];

    make_greeting(buffer);

    std::cout << "The greeting is \"" << buffer << "\"" << std::endl;

    for(int i = 0; i != ARRAY_ELEMENTS; i++)
        std::cout << "At position " << i << ": '" << buffer[i] << "'" << std::endl;
}

Of course, you should probably just be using std::string instead of character buffers anyways.




回答4:


I think the problem here is that this code

greeting[ARRAY_ELEMENTS]

doesn't do what you think it does. This tries to index into position ARRAY_ELEMENTS of the function greeting, which isn't a function. The error you're getting is caused by the compiler thinking you're trying to treat greeting as an array, which isn't what you want to do.

There isn't a direct fix for this, but you could consider doing one of the following:

  1. Change greeting to actually be an array.
  2. Make greeting a function that takes in an index and produces the appropriate array value.

Hope this helps!



来源:https://stackoverflow.com/questions/13771219/what-does-this-mean-c-warning-pointer-to-a-function-used-in-arithmetic-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!