First, make sure to post your code in [CODE] tags next time. Its difficult to read otherwise.
Code:
for ( i = 0; i < 20; i++ )
The loop will execute 20 times (0, 1, ..., 19, all inclusive). In the "for" loop we're calling a function called "AddOne", which we can assume will "add one" to the argument, which is "num". Since the initial value of "num" is 5, and we add one to it, twenty times, the result will be 5 + 1*20 = 25. Next,The "&" means to pass the address of the variable "num". This is usually done when you want to pass the variable to a function and have the function modify the variable. Next,
Code:
void AddOne( int *myVar ) {
Declaring a function, which expects to be passed the address of the argument "myVar". Finally,When you want to access the contents of a pointer (i.e. the value that the address points to), you have to dereference it, this is done simply with "*", so "*myVar" says "go to the address that myVar points to and get the value stored there". The very first time "AddOne" is called, the value of "myVar" would be an address, something like "0xf123abc" or whatever. The value that this address points to, i.e. "*myVar", will be 5. The "++" is the postincrement operator. It "adds one" to the value, so that after this line, "myVar" will, of course, still have the same value (the address), while the value stored at that address (*myVar) will have changed to 6.
Modify the program so that the definition of "AddOne" is instead this
Code:
void AddOne( int myVar );
, and it is instead called this way, and implemented this way
Code:
void AddOne( int myVar ) {
myVar++;
}
Do you understand why the output is what it will be? (5)