0

I have a C++ function in which I have two int's, who's purpose is to serve as counters, that are declared outside of the function in my main code. My goal is to update the counter variables with the result from the execution of the function.

I have them declared as such

int cor_letters = 0;
int cor_place = 0;

and then call my function like

res = compare(input, secret_word, &cor_letters, &cor_place);

My compare function header is:

bool compare(string user_input, string secret, int * correct_letters, int * correct_place)

and in my compare code, when I get the final values of the counters, I update them as such:

correct_letters = &cor_l;
correct_place = &cor_p;

I arrived at this solution after carefully reading through my compiler errors, and this seems to work. However, I don't quite understand why this works. In the beginning, I take the address of the two variables and pass them into the function. But the function takes two pointers. So the pointers point to the address of the passed in variables.

Up to this point I seem to grasp what's going on. But its the final assignments that I'm confused by - the pointers (note they're the var names from the function header) are then being updated to the address of the temporary inner function variables that I'm using. Why does this get me the values?

I'm more of a visual learner, and pointers are hard to grasp by just reading some text, so if you wouldn't mind making some quick text diagram to represent what's going on, that would be great. Thank you

walnut
  • 21,629
  • 4
  • 23
  • 59
danielschnoll
  • 3,045
  • 5
  • 23
  • 34
  • 2
    Pointers and references are different things in C++. Since you only talked about pointers in the question body, I have edited your question title to also do so. Please reedit if I mistook your intention. – walnut Oct 22 '19 at 07:24
  • Please don't describe the code and only show small excerpts. Instead try to create a [mcve] to show us. And if you get build errors, then please make sure that the example you show replicates the errors, and add comments on the lines where you get the errors. And of course include the actual errors you get, copy-pasted in full and complete. – Some programmer dude Oct 22 '19 at 07:24
  • Also please take some time to refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 22 '19 at 07:25
  • 2
    While the compiler might have stopped complaining, the resolution you are showing here is unlikely to actually do what you expect it to do, but without [repro] is is hard to tell for sure. Generally in C++, the compiler not complaining does not even mean to your program is free of definite errors (e.g. [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior)). – walnut Oct 22 '19 at 07:28
  • @uneven_mark You are correct, in that my code wasn't doing what I expected. The values weren't being updated outside of the function. See my accepted response below. – danielschnoll Oct 22 '19 at 08:02
  • Also @Someprogrammerdude , I felt this was minimal enough to explain my issue, as the entirety of my function wasn't necessary to convey I needed help with pointer logic – danielschnoll Oct 22 '19 at 08:03

1 Answers1

7

I guess you ended up with

correct_letters = &cor_l;
correct_place = &cor_p;

in order to make the compiler stop complaining.
Your analyse about taking the address of local variable is correct.

You probably want to do this

*correct_letters = cor_l;
*correct_place = cor_p;

in order to assign the correct values to the variables which are outside the function.

A brief memo about reference (&) and dereference (*) operations.

TYPE var_a=..., var_b=...; // some variables of a chosen type
TYPE *ptr=NULL;    // a pointer on such a type, but not referencing anything yet

ptr=&var_a; // now ptr memorises the address of var_a (reference operation)

var_b=*ptr; // access the value which is stored at the address memorised
            // by ptr (dereference operation) in order to read it
            // (here, this has the same effect as   var_b=var_a;   )

*ptr=var_a+var_b; // access the value which is stored at the address memorised
                  // by ptr (dereference operation) in order to alter it
                  // (here, this has the same effect as   var_a=var_a+var_b;   )
prog-fh
  • 13,492
  • 1
  • 15
  • 30
  • 2
    May also help to add a brief explanation that `'*'` is serving as the *dereference operator* to access the object at the address as opposed to the address itself. May also help to explain with pointers to arrays the `[...]` serves as a dereference as well. (you answer is good, these are just thoughts for improvement, not criticisms) – David C. Rankin Oct 22 '19 at 07:47
  • Well this wound up fixing it, turns out the values weren't being updated outside of the function. Also @DavidC.Rankin, that makes sense. Since `correct_letters` is a pointer containing an address, by doing `*correct_letters`, we dereference it and say the object at this address gets the value from `cor_l`, etc. – danielschnoll Oct 22 '19 at 08:00
  • @DavidC.Rankin Thank you for your advice. I tried to illustrate reference/dereference operations. I didn't take the time to explain arrays; I think that the question was more focused on accessing the calling context than on using arrays. A book would certainly be better to explain all of this ;^) – prog-fh Oct 22 '19 at 09:43