# Thread: Help with understanding Pointers...

1. ## Help with understanding Pointers...

Can anyone explain who the answer of 512 was reached for this code?

int gNumber;
int MultiplyIt( int myVar );

int main (int argc, const char * argv[]) {
int i;
gNumber = 2;

for ( i = 1; i <= 2; i++ )
gNumber *= MultiplyIt( gNumber );

printf( "Final value is %d.", gNumber );

return 0;
}

int MultiplyIt( int myVar ) {
return( myVar * gNumber );
}

2. What's the question? The first loop, it multipies 2 * 2 * 2, which sets gnumber to 8. Then i is incremented to 2 and the loop iterates again, which makes it 8 * 8 * 8, as gnumber is a global value, and 8 * 8 * 8 = 512. The loop ends and the value is printed and the program returns to the OS.

Wheres' the confusion?

3. If you are confused by what is happening, I suggest taking a pen and write down the value of gNumber for each iteration of the code. Then it should be clear what is going on. There are no pointers in the code BTW.

4. ## The Confusion

The confusion starts with this line:

gNumber *= MultiplyIt( gNumber );

in plain english, what does it mean step by step?

5. It means: gNumber = gNumber * MultiplyIt( gNumber );

It will assign the value that MultiplyIt returns times the value of gNumber to gNumber.

6. ## Barely grasping it but let's see.

As I am trying to learn C from an ebook as a precursor to developing with Objective C I can see that I must have gone over some of the initial basics a little too quickly to not be getting this.

Maybe if I try and explain what my interpretation is someone will spot the gaping whole in my logic. By the way, I'm sure you are all explaining it very well so thanks for all your help so far.

[int gNumber;] prototype definition for the compiler to be prepared for what's coming.
[int MultiplyIt( int myVar );] prototype definition for MultiplyIt with int myVar as a parameter.

[int main (int argc, const char * argv[]) {] the program starts...
[int i;] i is declared
[gNumber = 2; ] the global variable is set with a value of '2'.

[for ( i = 1; i <= 2; i++ )] a counter is initiated starting with a value of 1, it is then checked against the condition to see if it is true, it is still less than 2 so 1 is added to it to make it 2.

Am I right so far? where does it go next?
[gNumber *= MultiplyIt( gNumber );]

printf( "Final value is %d.", gNumber );

return 0;
}

int MultiplyIt( int myVar ) {
return( myVar * gNumber );
}

7. If you look at this: gNumber *= MultiplyIt( gNumber );
it's just a commonly used shorthand for writing: gNumber = gNumber * MultiplyIt( gNumber );

read it from right to left, MultiplyIt gets gNumber (2) as its argument, which means it will return 4, which gets multiplied by gNumber (2), so gNumber is now 8. In the next iteration of the for loop, the function returns 64 which is then mulitplied by 8, hence 512.

As I said use a pen and paper, you could then look at the first iteration of the for loop like this: gNumber = (2 * MultiplyIt(2)); to solve this look inside MultiplyIt(), it will return 4, which is multiplied by 2 = 8. In the next iternation of the loop substitute 2 with the new value of gNumber which is 8: gNumber = (8 * MultiplyIt(8)); so gNumber will now be 512 as MultiplyIt will return 64.

8. Contrary to the subject line, the code performs no pointer operations whatsoever.

9. Originally Posted by kiddo
[int gNumber;] prototype definition for the compiler to be prepared for what's coming.
I think this is your misunderstanding...gNumber is not a prototype it is a global variable declaration indicating that gNumber is an uninitialized integer accessible to all functions within the same file scope as itself.

So when you say gNumber inside your MultiplyIt() function it is the same as your gNumber inside main(). I guess that's your problem? You didn't explain your interpretation of the rest of the code.

10. ## ok just about there...

ok thanks,

but when is the code shown below called upon in the sequence?

int MultiplyIt( int myVar ) {
[return( myVar * gNumber );] what value is here and why?

and does the first pass through the for loop end with a value of 2? Is that result of 2 carried down and used here?

11. Originally Posted by kiddo
ok thanks,

but when is the code shown below called upon in the sequence?

int MultiplyIt( int myVar ) {
[return( myVar * gNumber );] what value is here and why?
It's called inside the loop, once for each iteration. The value of myVar is the same as gNumber as that is what you are giving as argument.

Originally Posted by kiddo
and does the first pass through the for loop end with a value of 2? Is that result of 2 carried down and used here?
You assigned 2 to gNumber, so that is what is passed to the function in the first iteration yes.

12. ## Let's try this again.

I am obviously missing some key pieces in terms of understanding how the flow because I'm still lost. The pen and paper idea is good. I have tried that with simpler program and been able to follow along but I there are some things that just don't make sense to me.

In the function that is being called does myVAr equal whatever MultipyIt is? Pretend I'm mentally challenged (which is how I feel right now and do an explanation of every single line. I get your explanation of the shorthand for[(gNumber *= MultiplyIt( gNumber ),] but I'm not following the flow from the time we hit the for loop for the first time until the end of the program.

int gNumber;
int MultiplyIt( int myVar );

int main (int argc, const char * argv[]) {
int i;
gNumber = 2;

for ( i = 1; i <= 2; i++ )
gNumber *= MultiplyIt( gNumber );

printf( "Final value is %d.", gNumber );

return 0;
}

int MultiplyIt( int myVar ) {
return( myVar * gNumber );
}

By the way. I decided to try and learn C so that I could use Objective C and Cocoa to develop iphone applications for kids. I am a beginner in the truest sense and personally I would have loved to just skip C but i figure Objective C will go easier if I understand the basics of C. Are they very different?

13. myVar will get the number you pass to the function so for example: MultiplyIt(5); would set myVar to 5. Perhaps you should take one step back and look at loops and functions separately to get the concept.

On objective-C, it's built on C and is an expansion of it if you will, which gives object oriented features to C. The syntax is a bit special with a lot of [] for method calls. I personally think that you will benefit from having a basic understanding of C with you, as well as some knowledge about object orientation in general. But I know that some believe that it's not necessary and proceed to learn objective-C directly. Another thing to remember is that not all Apple API's are objective-C, and you might end up needing one of those API's one day and then it will be useful to know something about C obviously.