Search code examples
c++pointersroot-framework

When to pass a pointer and when to pass the memory address of a pointer?


I am looking at the following piece of code:

  StJetEvent* jets = 0;
  jetChain->SetBranchAddress("AntiKtR060NHits12",&jets);

Where we have that: The class of jetChain is TChain and the definition of SetBranchAddress is:

Int_t TChain::SetBranchAddress  (   const char *    bname, void *   add, TBranch **     ptr = 0)        

The definition of the relevant parameters inside of the the argument of the SetBranchAddress is:

bname is the name of a branch. add is the address of the branch.

For some reason, when I look at SetBranchAddress("AntiKtR060NHits12",&jets), based on the definition of the function SetBranchAddress, I would think that the second parameter that needs to be passed is a pointer, but instead the address of the pointer jets is passed. This is consistent with what the definition of what the second parameter is, but from my basic understanding, I thought that void * add means to pass a pointer and NOT the address of the pointer.

Can someone please provide me with some clarification, please? Thank you very much!


Solution

  • The syntax &z returns a pointer to z. The terms "address of X" and "pointer to X" mean almost exactly the same thing.

    int z;
    int* q = &z; // now q is a pointer to z
    int** r = &q;
    

    When we say "q is a pointer to z", we mean two things:

    1. The type of 'q' is pointer to whatever type 'z' is.
    2. The value of 'q' is the address of 'z'.

    So here, 'r' is a pointer to 'q' for the same reasons.