1

I'm trying to get the hang of pointers and addresses in C++ and am having trouble with functions with changing parameters.

The code below is writing Loop run #1. in an infinite loop, instead of incrementing the value foo.

My question is: What is the issue with this code here?

#include <iostream>

void Statement(int *foo) {
    std::cout << "Loop run #" << *foo << ". ";
    foo++;
}

int main() {

    int foo = 1;

    for (;;) {
        Statement(&foo);
    }

}
peki
  • 669
  • 7
  • 24
  • What is the code doing? What is it supposed to do? – Paul Belanger Jun 28 '19 at 15:28
  • Your problem has nothing to do with functions. It would be the same if all the code was inside main. – drescherjm Jun 28 '19 at 15:28
  • 5
    Dis you mean to write `(*foo)++;` inside the function? – πάντα ῥεῖ Jun 28 '19 at 15:29
  • 2
    You are incrementing a copy of a pointer, not the integer being pointed to – AndyG Jun 28 '19 at 15:30
  • 4
    In `c++` its better to pass by reference instead of by pointer like you would have if you were writing `c` code. – drescherjm Jun 28 '19 at 15:30
  • Thank you to πάντα ῥεῖ and AndyF for clearing up the mishap :) – peki Jun 28 '19 at 15:31
  • @VladfromMoscow I ran the code through and it works just fine. The goal expressed in the post has been reached with both your and Sombrero's/Paul's methods. Thanks for the help in any case! – peki Jun 28 '19 at 15:47
  • @maxxy Oh, I am sorry. I thought that you are returning the expression ( *foo )++.:) – Vlad from Moscow Jun 28 '19 at 15:49
  • 1
    @maxxy Regarding your question as posted. _**"What is the issue with this code here?"**_ Isn't a clear statement about the problem you have. Always explain clearly what you expected from the (also clearly specified) inputs and outputs you see. In other word's post a [mcve], this is the minimum required for well received questions here. – πάντα ῥεῖ Jun 28 '19 at 15:57
  • I'll take care to be more descriptive and detailed with my questions! – peki Jun 29 '19 at 09:34

3 Answers3

8

You're incrementing a copy of the pointer itself, not what it points to. You probably meant:

(*foo)++;

This still won't fix the infinite loop though because you have nothing to stop it with.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
5

Your issue is that you're incrementing the pointer, not the pointed-to data.

replace

foo++

with

(*foo)++

to increment the pointed-to value.

Paul Belanger
  • 2,354
  • 14
  • 23
1

If I have understood correctly what you are trying to do then the function should be declared the following way as it is shown in the demonstrative program

#include <iostream>

void Statement(int *foo) {
    std::cout << "Loop run #" << *foo << ". ";
    ++*foo;
}

int main() {

    int foo = 1;

    for (; ; ) {
        Statement(&foo);
    }
}

That is in an infinite loop you are trying to output incremented value of foo.

In this case you have increment the value itself pointed to by the pointer like

++*foo

If you want to limit loop iterations then you can use for example an object of the type unsigned char and define the loop the following way

#include <iostream>

void Statement( unsigned char *foo) {
    std::cout << "Loop run #" << int( *foo ) << ". ";
    ++*foo;
}

int main() {

    unsigned char foo = 1;

    for (; foo ; ) {
        Statement(&foo);
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335