Question

I found a code snippet on the internet. When I compile and run it, the output is 70. but i don't know whats happening in the code. please help me out.

#include <iostream>
using namespace std;  

void doubleNumber (int *num ) 
{
    *num = *num * 2;
} 
int main () 
{ 
    int num = 35;
    doubleNumber (&num) ; 
    cout <<num ;  
    return 0; 
}
Était-ce utile?

La solution

void doubleNumber (int *num ) takes a pointer to an integer as parameter, which permits the method to modify the original variable.

Calling *num dereferences the pointer, while *num = *num * 2 assigns the value of the variable of the pointer num multiplied by 2 to the memory cell where num points to.

And in the main, where you have declared the integer, by calling the function doubleNumber with &num, you reference the variable and the return value of that is the pointer to the variable.

int num = 35;
doubleNumber(&num); 

Is equivalent to:

int num = 35;
int* num_pointer = &num;
doubleNumber(num_pointer); 

You should probably take a look at this site to read about referencing and dereferencing.

Autres conseils

In your main function you call doubleNumber() passing a pointer to num. The doubleNumber() function receives the pointer and doubles his value.

*num = *num * 2

The code defines a function which "doubles" an number. The main program passes the pointer to the variable num in to the function, and the function doubles the variable using the pointer passed in.

Pointers in C++ are my favorite (and - for newer programmers - are often confusing because they are learned in tandem with referencing (& operator). The * symbol is used for a lot of 'stuff' in C++, and it is not helped by the fact that, with pointers, the * symbol does two different things, for which we have two (2) names: dereferencing and indirection.

When we declare a pointer to a type, e.g. int *intPtr = //CODE HERE, we enable the variable to accept an address *intPtr and assign the address in memory of the rvalue (that on the right side of the binary operator) to the variable intPtr. intPtr - or, the address of the rvalue - can then, itself, be passed around and used as an lvalue. We call this "dereferencing".

Then, when we want to access the value of the thing stored at the memory address, we use the indirection operator * in the body of the code to access the stored value. So, let's say we do this:

int num = 35;
int num2 = 0;
int *intPtr = &num; // here using the reference operator & to assign the address
                    // of num to intPtr

We can then access the value stored behind num by going:

num2 = *intPtr; 

Here, the * indirection operator is actually doing something else: it is used to access the value stored at the address stored in intPtr.

WHERE THE CONFUSION HAPPENS:

So, when we see a function header with a pointer as an argument, it's like "Wha'? Which * is being used?"

returntype functionIdentifier(type *num)  

In this case, what is received as an argument is a memory address. Then, the argument can be used throughout the body of the function. Too, the indirection operator * can be used to access the value stored at the memory address stored in the passed-in argument (pointer - in this came num).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top