Thread: Question from a Newbie

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

    Question from a Newbie

    I'm working my way slowly through a book called 'Learn C on the Mac' and I am doing the exercises at the end of the chapter. They ask you to predict the result based on being able to follow the code and understand it. The answer is 25 but I don't get how they managed to get that result. Obviously I won't ever understand the basics if I don't take the problem as far as I can before getting help. So what I have done is paste first the code they gave me and then the code again but with my notes on what of it that I understand....

    void AddOne( int *myVar );

    int main (int argc, const char * argv[]) {
    int num, i;

    num = 5;

    for ( i = 0; i < 20; i++ )
    AddOne( &num );

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

    return 0;
    }

    void AddOne( int *myVar ) {
    (*myVar) ++;
    }


    And now my interpretation of the code...

    void AddOne( int *myVar ); <-- Here is where the AddOne function is declared with a variable that is actually a pointer to myVar.

    int main (int argc, const char * argv[]) { Main will be an integer.
    int num, i; declaration of the variable 'mum' which is an integer

    num = 5; initialization of the variable 'num' to a value of '5'

    for ( i = 0; i < 20; i++ ) a for loop that starts at 0 and will continue to go through the loops until it reaches 19.
    AddOne( &num ); The function AddOne is called so we go to the code of the function next...

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

    return 0;
    }

    void AddOne( int *myVar ) { And now I am lost!!
    (*myVar) ++;
    }


    If anyone could please please walk me through the last part I would greatly appreciate it. I figure the sticking point has something to do with being a little unclear on pointers so I'm interest to see what you think.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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,
    Code:
    AddOne( &num );
    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,
    Code:
    (*myVar) ++;
    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
    Code:
    AddOne( num );
    , and implemented this way
    Code:
    void AddOne( int myVar ) {
      myVar++;
    }
    Do you understand why the output is what it will be? (5)
    Last edited by nadroj; 02-27-2010 at 01:15 PM.

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

    Still not quite there...

    so [ (*myVar) ++; ] is the same as saying 5 + 1 (or 6) which is then sent back up to be added to 19 to get and answer of 25?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    (*myVar) ++;
    can be considered the same as
    Code:
    *myVar = *myVar + 1;
    In the same way that
    Code:
    int x = 1;
    x++; // can be considered equivalent to:
    x = x + 1;

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

    I understand the last part but....

    Ok. So thanks for explaining that that last part. I reconstructed the code from the example that you were trying to use to help me understand how the program and this is what I put together:

    void AddOne( int myVar );

    int main (int argc, const char * argv[]) {
    int num,

    num = 5;

    AddOne( num );

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

    return 0;
    }

    void AddOne( int myVar ) {
    myVar++;
    }

    Is that what you were trying to get me to alter the code to? How'd you get 5 from that. If that's not how the code is supposed to look can you show me the code so I can see how you reached 5 as your answer?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well I didnt say to remove the "for" loop, but the result should be the same. Did you run it? The result should be 5.

    (almost) All parameters to functions are passed by value. This means that when you call "AddOne", a copy of "num" is passed to the function. So when, inside the "AddOne" function, it increments "myVar" by one ("myVar++"), the value of myVar is now 6. However, this doesnt have anything to do with the variable or value of "num" in main. "AddOne" modified a copy of "num"--"num" and "myVar" are two entirely different variables. So since "AddOne" doesnt have any effect on "num", "num" stays the same and it would output "5".

    The same thing with pointers. When you pass "&num" to the previous versino of "AddOne" that accepted an "int *", a copy of the value is passed to "AddOne". In this case, the value is "&num", the address of "num". Now we have the variable "num" in main, and we have a copy of its address in "myVar" in "AddOne". Since we have the address, we can directly access and modify the value (via "*myVar"). The "(*myVar)++", again, says "go to the value pointed to by the address "myVar" and increment it by one". Since it modified the value at that address, it indirectly modified "num".

    It would be difficult, or at least take a while to fully explain this concept. But it is very important, and you should try and look up a basic tutorial or explanation, preferably with graphical examples to understand it. One on this site is Pointers.

    Let us know if you have questions
    Last edited by nadroj; 02-27-2010 at 04:48 PM.

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

    You are awesome!

    Thanks. I've been working away at this for hours so I am going to revisit it tomorrow, check out the pointers tutorial and I will send you a reply if I have any questions.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM