Thread: Help with understanding Pointers...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    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. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    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. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    The Confusion

    The confusion starts with this line:

    gNumber *= MultiplyIt( gNumber );

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

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It means: gNumber = gNumber * MultiplyIt( gNumber );

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

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    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. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.
    Last edited by Subsonics; 03-07-2010 at 05:52 PM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Contrary to the subject line, the code performs no pointer operations whatsoever.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by kiddo View Post
    [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. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    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. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by kiddo View Post
    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.

    Quote Originally Posted by kiddo View Post
    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. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    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. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.
    Last edited by Subsonics; 03-07-2010 at 06:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. understanding pointers
    By princez90 in forum C Programming
    Replies: 14
    Last Post: 04-19-2008, 09:56 AM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM